-3

New to python and creating my first program. I've tried running it and everything works fine, but I want to add a third option to the "yes" or "No" question. So if they say answered "moose", could I get the program to reject that answer and ask the question again?

Any help would be greatly appreciated.

#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()
Hellfire
  • 95
  • 1
  • 6
  • 2
    Possible duplicate of [I want to have a yes/no loop in my code, but I'm having trouble doing it (python 3.3)](https://stackoverflow.com/questions/22362165/i-want-to-have-a-yes-no-loop-in-my-code-but-im-having-trouble-doing-it-python) – Ken Y-N Feb 28 '19 at 05:21
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Feb 28 '19 at 08:06

4 Answers4

0

I like to use a helper method for this sort of thing

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

then you could do stuff like

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

you can also make other helper methods

def get_int(prompt):
    while True:
         try:
            return int(input(prompt))
         except:
            print("Please enter an integer")
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Create a list of options user_input = ['yes', 'YES', 'Y', 'No', 'N', 'NO'] and check if input is in user_input:, if it is then proceed with you code else break the program and print that Wrong input.

Alternatively, you can also use Try and Except.

Amit Gupta
  • 2,698
  • 4
  • 24
  • 37
0

you can use a while loop to keep asking the question until you don't get one of the two possible options.

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

I really like Joran's solution for the modularity, but if you're looking for a quick hack to your own code just to understand conditionals. You can add a boolean variable that keeps being reset if the user gives an improper input.

With little change to your original code, this will handle improper input across all the user prompts:

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()
LeKhan9
  • 1,300
  • 1
  • 5
  • 15