python检查特定单词的字符串

2024-04-20 08:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做的是,如果我问了一个只能用是和回答的问题 不,我想确定答案是肯定还是否定。否则,它将停止

 print("Looks like it's your first time playing this game.")
 time.sleep(1.500)
 print("Would you like to install the data?")
 answer = input(">").lower
 if len(answer) > 1:

    if answer == "no":
        print("I see. You want to play portably. We will be using a password system.")
        time.sleep(1.500)
        print("Your progress will be encrypted into a long string. Make sure to remember it!")
    else:
        print("Didn't understand you.")

elif len(answer) > 2:

    if word == "yes":
        print("I see. Your progress will be saved. You can back it up when you need to.")
        time.sleep(1.500)

else:
    print("Didn't understand you.")

Tags: toansweryouyourleniftimeit
2条回答

比如:

if word.lower() in ('yes', 'no'):

这是最简单的方法(假设情况无关紧要)

旁注:

answer = input(">").lower

正在将对lower方法的引用分配给answer,而不是调用它。您需要添加paren,answer = input(">").lower()。另外,如果这是python2,则需要使用raw_input,而不是input(在python3中,input是正确的)

首先:

answer = input(">").lower

应该是

answer = input(">").lower()

第二,len(answer) > 1"no""yes"都是正确的(就这点而言,大于一个字符的任何字符)。elif块永远不会被计算。在不显著修改当前代码逻辑的情况下,您应该改为:

if answer == 'no':
    # do this
elif answer == 'yes':
    # do that
else:
    print("Didn't understand you.")

相关问题 更多 >