eescence
eescence

Reputation: 3

Python incorrect answer input

I would like to make it so my menu works in a way which if a letter is input or anything other than a correct answer is input my script doesn't just end abruptly and it asks to input a correct option. Could anyone help me do this please? Here is the code i have so far:

#variables for password entry
secret_word = "giraffe"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

#password entry code
while guess != secret_word and not(out_of_guesses):
    if guess_count < guess_limit:
        guess = input("enter guess: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("out of guesses!")
else:
    print("You are into the secret lair!")

#Menu code

def menu(menu):
    print("--------------------------\nMenu\n--------------------------\n1.Secret 
Sauce\n2.More secret stuff\n3.Even more secret stuff\n4.Exit")
    choice = int(input("--------------------------\nENTER CHOICE: "))
     if choice == 1:
        print("--------------------------\nSecret Sauce recipe:\n1.ITS A SECRET!")
    elif choice == 2:
        print("--------------------------\nThis is also secret! Go away!")
    elif choice == 3:
    print("--------------------------\nYOU DARE TO TRY AGAIN?! STOP, GO AWAY!")
    elif choice > 4:
     print("This was not an option! TRY AGAIN!")
        return (menu)
    else:
        return(False)
    return(choice)

#exit loop for the def

running = True
while running:
    menuChoice = menu(menu)
    if menuChoice == False:
        print("\nGoodbye")
        running = False

Upvotes: 0

Views: 82

Answers (2)

Eden Yosef
Eden Yosef

Reputation: 39

Instead of choice = int(input("--------------------------\nENTER CHOICE: "))

You can do the following: choice = input("--------------------------\nENTER CHOICE: ")

and then after it we need to check if the string is a digit, so you can transfer it to "int" type safely.

if choice.isdigit():
    if choice == 1:
        print("--------------------------\nSecret Sauce recipe:\n1.ITS A SECRET!")
    elif choice == 2:
        print("--------------------------\nThis is also secret! Go away!")
    elif choice == 3:
        print("--------------------------\nYOU DARE TO TRY AGAIN?! STOP, GO AWAY!")
    elif choice > 4 or choice != (1,2,3,4):
        print("This was not an option! TRY AGAIN!")
    return (menu)
else:
    return(False)
return(choice)

Upvotes: 1

Gavin Wong
Gavin Wong

Reputation: 1270

In your menu() function,

Instead of immediately casting the input into an int, you can use a try statement to make sure the input is an integer, so if the user types in a letter your program will not stop. For example:

validInput = False
while validInput == False:
    choice = input("--------------------------\nENTER CHOICE: ")
    try:
        choice = int(choice)
        validInput = True
    except:
        print("Please enter an integer.")

Replace choice = int(input("--------------------------\nENTER CHOICE: ")) with the above code and it will work.

Upvotes: 0

Related Questions