使用python回答yes或no问题的第三个答案

2024-10-03 06:20:04 发布

您现在位置:Python中文网/ 问答频道 /正文

不熟悉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()

Tags: tonogoifisexitityes
3条回答

我喜欢用helper方法来处理这类事情

def ask_yes_no(prompt):
    """
    continue prompting user until they enter yes or no
    return True if user enters "yes" else it will return False
    """
    while True:
        result = input(prompt)
        if result.lower() in ["yes","no"]:
           return result.lower() == "yes"
        print("please enter YES or NO.")

if ask_yes_no("Do Something?"):
   print("User says YES!")
else:
   print("User Says NO")

然后你可以做类似的事情

def is_it_raining():
    return ask_yes_no("Is it raining outside?")

if is_it_raining():
   print("Its Raining... play some games")
else:
   print("Its sunny, play outside!")

您还可以创建其他助手方法

def get_int(prompt):
    while True:
         try:
            return int(input(prompt))
         except:
            print("Please enter an integer")

创建一个选项列表user\u input=['yes'、'yes'、'Y'、'No'、'N'、'No']并选中 如果输入是在用户输入:,如果是,那么继续你的代码,否则打破程序和打印错误的输入。你知道吗

或者,也可以使用Try和Except。你知道吗

你可以使用while循环不断地问这个问题,直到你没有得到两个可能的选项中的一个。你知道吗

#Is it raining outside?

raining = "yes"
umbrella = "yes"
stillraining = "yes"
israining = "yes"
landoctopus ="yes"#This was supposed to be "haveumbrella", but for some reason that's not allowed but "landoctopus" is?
isitraining = ""
while (not (isitraining == "yes" or isitraining == "no")):
    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("")
    landoctopus = input()
    if landoctopus == "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 landoctopus == "yes":#If answer is yes, tell them to "Go outside."
        print("")
        print("Go outside.")
        exit()

相关问题 更多 >