Try and Except issue with range

Viewed 15

My error handling is stuck, I keep getting the error that unindented amount does not match.

def landing():
    try:
        print("1) Create a new member")
        print("2) Check for fines")
        print("3) Check overdues")
        print("4) Check for available services")
        print("5) Exit: ")
        choice = input("Please enter a number from the menu:")
        if choice in range (1,6):
     except ValueError:
        print("choice must be a number from 1 -5")
1 Answers

Be aware that the input built-in function is taking a string from whatever answer the user is providing. For your case, you need to transform this input into an integer for this case.

Try this:

def landing():
    print("1) Create a new member")
    print("2) Check for fines")
    print("3) Check overdues")
    print("4) Check for available services")
    print("5) Exit: ")
    choice = int(input("Please enter a number from the menu:"))
    while choice not in range (1,6):
        print("choice must be a number from 1 -5")
        choice = int(input("Please enter a number from the menu:"))
    
    return choice
    
landing()
Related