使用python的是或否问题的第三个答案

时间:2019-02-28 05:14: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()

4 个答案:

答案 0 :(得分:0)

我喜欢对此类事情使用辅助方法

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")

答案 1 :(得分:0)

创建一个选项列表user_input = ['yes','YES','Y','No','N','NO']并检查 如果输入在user_input:中,如果输入正确,则继续执行代码,否则中断程序并打印错误的输入。

或者,您也可以使用尝试并排除

答案 2 :(得分:0)

您可以使用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()

答案 3 :(得分:0)

我真的很喜欢Joran的模块化解决方案,但是如果您想快速了解自己的代码只是为了了解条件。您可以添加一个布尔变量,如果用户输入的内容不正确,该变量将一直被重置。

只需对原始代码进行少量更改,即可处理所有用户提示中的不正确输入:

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?

correct_option = False

# helper to use for validity, used throughout to bring back user to the right flow
def is_valid(val):
    return val in ["yes", "no"]


while not correct_option:
    print("Is it raining outside?")
    print("")

    correct_option = True

    isitraining = input()

    if not is_valid(isitraining):
        correct_option = False

    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 not is_valid(landoctopus):
            correct_option = False

        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()

                if not is_valid(stillraining):
                    correct_option = False

                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()