Skip to main content
 首页 » 编程设计

Python 输入函数

2025年05月04日10bjzhanghao

我想知道是否有人可以告诉我这段代码有什么问题,当我运行代码时它什么也没显示,但如果我去掉“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")