我创建了一个简单的问答程序。我创建了一个包含值a,b,c和d的列表。
# Declare list of choices
choices = ['a','b','c','d']
# Create a simple question and various answers
print("What comes up, must come...?")
print("a. sideways.")
print("b. up.")
print("c. left.")
print("d. down.")
user_answer = input("Choose the best answer: ")
# Determine if the user typed a valid choice while user_answer.lower() != choices[0] and user_answer.lower() != choices[1] and user_answer.lower() != choices[2] and user_answer.lower() != choices[3]:
user_answer = input("Sorry, %s is not a valid choice, try again: " % user_answer).lower()
# Determine if the input was the correct answer
if user_answer == 'd':
print("\n**Congrats! You got the right answer!**")
else:
print("\n--Sorry, You unfortunately got the answer wrong. --")
该程序运行良好,但无需输入...
while user_answer.lower() != choices[0] and user_answer.lower() != choices[1] and user_answer.lower() != choices[2] and user_answer.lower() != choices[3]:
相反,我想要这样的东西...
#where it's comparing all the values in the list without comparing each index value.
while user_answer.lower() != choices[x]
这可能吗?我敢肯定有一种更有效的方法。感谢您的时间和知识!