这是我试图循环的地方,如果他们不输入b或c他们将得到错误输出,因此将不得不回到问题。虽然这不起作用: 已更新
if get_bool_input("Do you wish to view the previous results from your class: "): #The 'get_bool_input' will determine if they input yes or no and will either open the file or not.
selection= input("Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)")
while True:
if selection not in ['A', 'B', 'C']:
print ("Error, type in A, B or C.")
break
if selection == 'A':
print (alphabetically(data))
break
elif selection == 'B':
print (by_score(data))
break
elif selection == 'C':
print (by_average(data))
break
else:
input ("Press any key to exit")
输出
Do you wish to view the previous results from your class: yes
Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)n
Error, type in A, B or C.
我没有选择输入另一个答案。
答案 0 :(得分:1)
您有四个问题:return True
在函数之外的语法无效,即使它位于elif
和else
之间,else: selection not in ['A', 'B', 'C']:
也不是有效的语法,您永远不会重新定义selection
。也许你想要更像这样的东西:
while True:
selection = input("Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)")
if selection not in ['A', 'B', 'C']:
print ("Error, type in A, B or C.")
continue
if selection == 'A':
print (alphabetically(data))
elif selection == 'B':
print (by_score(data))
elif selection == 'C':
print (by_average(data))
break
答案 1 :(得分:-1)
您的else
语句永远不会被执行。你的return True
语句会削减那里的执行流程。
您可能希望这样:
while True:
selection= input("Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)")
if selection == 'A':
print (alphabetically(data))
return True
elif selection == 'B':
print (by_score(data))
return True #replace this with 'break' if you're not inside a function
elif selection == 'C':
print (by_average(data))
return True
else:
print ("Error, type in A, B or C.")