所以,当我为了好玩而编写一个简单的刽子手游戏时,由于我是初学者,我仍然需要一些帮助:
我希望我的算法能够测试输入是否只有一个字符,例如:
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
假设我的 secret 词是
'billy'
我想知道如何检查这个输入的字母是否与 secret 单词中的任何一个字母相同。例如if x *code checking if the input has a same letter as the secret word*: #carry on program
如果您能帮助我解决这两个问题中的任何一个,您将成为我的救星!
最佳答案
检查时可以使用if in。因此,您获得输入,然后使用如下代码:
if x in secret_word:
#carry on with the program
要检查它是否有一个字符,请使用 len。所以
if len(x)==1:
#carry on
关于Python 刽子手算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197005/