elif和else命令被完全忽略,相反,无论我键入什么,即使是随机字母,我都会得到我的答案

2024-04-26 03:56:14 发布

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

在我完成下面的代码后,我检查了它是否有效。问题是,无论我键入什么,它总是显示“如果”的响应,即使我更改了响应

我试着改变立场和反应,但没有任何效果

hello = input ("Hello there! \n My name is PIM. \n It `enter code here` stands for Primary Interactive Machine. \n What is your name?")
print ("Nice to meet you," + hello)
ans1 = input ("Can I ask you a question? <yes/no>")
if (ans1 == 'yes',' yes')                              
    ans2 = input ("Which color do you prefer? <blue/pink>")
elif(ans1 == 'no',' no'):
    print ("Well then, we will just stay in silence")
else:
    print ("I will not talk to someone who doesn't care about me. \n You had one simple job, choose yes or no and even then, you failed.")

预期它会相应地回复响应,但它只发送“if”回复


Tags: tono代码nameyouhelloinput键入
1条回答
网友
1楼 · 发布于 2024-04-26 03:56:14

试试这个:

if ans1 in( 'yes' , ' yes'):                             
    ans2 = input ("Which color do you prefer? <blue/pink>")
elif ans1 in( 'no' or ' no'):
    print ("Well then, we will just stay in silence")
else:
    print ("I will not talk to someone who doesn't care about me. \n You had one simple job, choose yes or no and even then, you failed.")

相关问题 更多 >