gpt4 book ai didi

Python 输入函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:32 25 4
gpt4 key购买 nike

我想知道是否有人可以告诉我这段代码有什么问题,当我运行代码时它什么也没显示,但如果我去掉“elif”它确实有效。\

first=input("What is your first name? ");
middle=input("What is your middle name? ");
last=input("What is your last name? ");
test = [first, middle, last];
print ("");
print ("Firstname: " + test[0]);
print ("Middlename: " + test[1]);
print ("Lastname: " + test[2]);
print ("");
correct=input("This is the information you input, correct? ");
if (correct == "Yes" or "yes"):
print ("Good!")
elif (correct == "no" or "No"):
print ("Sorry about that there must be some error!");

最佳答案

问题是:

if (correct == "Yes" or "yes"):
# ...
elif (correct == "no" or "No"):
# ...

应该是:

if correct in ("Yes", "yes"):
# ...
elif correct in ("No", "no"):
# ...

请注意,进行涉及多个条件的比较的正确方法是这样的:

correct == "Yes" or correct == "yes"

但通常是这样写的,比较短:

correct in ("Yes", "yes")

关于Python 输入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com