我想知道是否有人可以告诉我这段代码有什么问题,当我运行该代码时,它什么也没有显示,但是如果我取出“ 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 - Python输入功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18987413/

10-09 19:14