Python刽子手算法

PyP*_*oob 2 python python-3.x

因此,当我编写一个简单的刽子手游戏以获得乐趣时,因为初学者我仍然需要帮助:

  1. 我希望我的算法测试输入是否只有一个字符,例如:

    while True:
      x=str(input())
      #code checking wheather it is only one character, if it is  
      character break and stop the loop, 
      otherwise repeat the input
    
    Run Code Online (Sandbox Code Playgroud)
  2. 让我说我的秘密词是'billy' 我想知道如何检查这一个字母输入是否与秘密词中的任何一个字母相同.例如

    if x *code checking if the input has a same letter as the secret word*:
      #carry on program
    
    Run Code Online (Sandbox Code Playgroud)

如果你可以帮助我解决这两个问题中的任何一个问题,你就可以成为救星!

K-D*_*D-G 5

您可以在检查时使用if.所以你得到输入然后你会使用如下代码:

if x in secret_word:
  #carry on with the program
Run Code Online (Sandbox Code Playgroud)

要检查它是否有一个字符使用len.所以

if len(x)==1:
  #carry on
Run Code Online (Sandbox Code Playgroud)