不熟悉python并创建了我的第一个程序。我试过运行它,一切正常,但我想在“是”或“否”问题上添加第三个选项。所以,如果他们说答案是“驼鹿”,我能让程序拒绝这个答案,然后再问这个问题吗?你知道吗
任何帮助都将不胜感激。你知道吗
#Is it raining outside?
raining = "yes"
umbrella = "yes"
stillraining = "yes"
israining = "yes"
spacestation ="yes"#This was supposed to be "haveumbrella", but for some reason that's not allowed but "spacestation" is?
print("Is it raining outside?")
print("")
isitraining = input()
if isitraining == "no":#Ask user if it is raining, if not, tell them to "Go outside" followed by exit statement
print("")
print("Go outside")
import sys
exit()
elif isitraining == "yes": #It is raining
print("")
print("Do you have an umbrella?") #Ask if they have an umbrella
print("")
spacestation = input()
if spacestation == "no":#If answer is no, tell them to "Stay inside and wait."
print("")
print("Stay inside and wait.")
print("")
while stillraining == "yes": # Ask use if it is still raining outside.
print("Is it still raining outside?")
stillraining = input()
print("")
print("Stay inside and wait")
print("")
if stillraining == "no":
print("")
print("Go outside")
exit()
elif spacestation == "yes":#If answer is yes, tell them to "Go outside."
print("")
print("Go outside.")
exit()
我喜欢用helper方法来处理这类事情
然后你可以做类似的事情
您还可以创建其他助手方法
创建一个选项列表user\u input=['yes'、'yes'、'Y'、'No'、'N'、'No']并选中 如果输入是在用户输入:,如果是,那么继续你的代码,否则打破程序和打印错误的输入。你知道吗
或者,也可以使用Try和Except。你知道吗
你可以使用while循环不断地问这个问题,直到你没有得到两个可能的选项中的一个。你知道吗
相关问题 更多 >
编程相关推荐