Home > Software design >  How do you provide an error when an invalid input is provided ie. (a color other than purple or sky
How do you provide an error when an invalid input is provided ie. (a color other than purple or sky

Time:12-04

while True:
    try:
        color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
        color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()
        user_info = {"color2": color1, "color2": color2,}
    except ValueError:
        print("Choose a valid input please...")
        continue
    else:
        break

Trying to get it to give an error and restart the loop but its not working.

CodePudding user response:

To check if the input is valid, you can use an if statement inside the try block to check if the input matches the expected colors. If the input is not valid, you can raise a ValueError to indicate that there was an issue with the input. Here is an example of how you can do this:

while True:
    try:
        color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
        color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()
        if color1 not in ["purple", "sky blue"]:
            raise ValueError("Invalid input for color1: {}".format(color1))
        if color2 not in ["white", "yellow", "pink"]:
            raise ValueError("Invalid input for color2: {}".format(color2))
        user_info = {"color2": color1, "color2": color2,}
    except ValueError as error:
        print("Error: {}".format(error))
        continue
    else:
        break

This code will check if the input for color1 is either "purple" or "sky blue", and if the input for color2 is either "white", "yellow", or "pink". If the input is not valid, a ValueError is raised with a message indicating which color had an invalid input. The except block will catch this error and print the error message, then continue the loop to prompt for input again. If the input is valid, the loop will break and the program will continue. I hope this helps. Feel free to ask if you have any other questions.

CodePudding user response:

That works:

while True:
    try:
        color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
        color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()
        user_info = {"color2": color1, "color2": color2,}
        valid_input1 = ['purple', 'sky blue']
        valid_input2 = ['white', 'yellow', 'pink']
        if color1 not in valid_input1 or color2 not in valid_input2:
            raise ValueError
    except ValueError:
        print("Choose a valid input please...")
        continue
    else:
        break

You could also do something like this:


color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()
user_info = {"color2": color1, "color2": color2,}

valid_input1 = ['purple', 'sky blue']
valid_input2 = ['white', 'yellow', 'pink']

while color1 not in valid_input1 or color2 not in valid_input2:
    print("Choose a valid input please...")
    color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
    color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()

CodePudding user response:

You aren't raising ValueError, you can change your code to check for invalid input using an if statement for each input:

while True:
    color1 = str(input("What should the color of the broken window be, Purple or Sky Blue? > ")).lower().strip()
    color2 = str(input("What should the color of the broken window, white Yellow or Pink? > ")).lower().strip()
    user_info = {"color2": color1, "color2": color2,}
    if color1 not in ["purple", "sky blue"] or color2 not in ["yellow", "pink"]:
        print("Choose a valid input please...")
        continue
    break

Also if you are using Python 3, then input returns a string, so passing it to str isn't needed.

  • Related