2016-04-23 55 views
-1
print_list=input("Do you wish to print list \n:") 

if print_list == "yes": 
    for item in List: 
     print (item , "%2.f" %(Speed),"m/s") 

elif print_list == "no": 
    print ("Thank you") 

if print_list != "yes" or "no": 
    while True: 
     print ("Invalid Input") 
     break 

这是发生了什么事:循环当条件不符合程序

Do you wish to print list 
:hhh 
Invalid Input 
Press "Enter Key" when the vehicle passes Sensor 1 
: 

我希望是该方案提出这样的问题:“你希望打印清单”,当用户输入无效。

+0

'while true:'应该包围所有要重复的代码 –

回答

2

如果你想让第一个语句再次发生,那么它必须在循环中。基本上所有这些都需要在while循环中。中断会导致循环结束,所以绝对不要在无效输入之后放置它,因为您希望再次询问用户。

0
while True: 
    print_list=input("Do you wish to print list \n:") 

    if print_list == "yes": 
     for item in List: 
      print (item , "%2.f" %(Speed),"m/s") 
     break 

    elif print_list == "no": 
     print ("Thank you") 
     break 

    else: 
     print ("Invalid Input\n")