Error handling

Asked 2 years ago, Updated 2 years ago, 111 views

answer = 20

while True:
    try:
        guess = input("Enter a number bet. 1 and 100: ")

        If guesses > 100 or guesses < 1: # If entered out of range
            print("only enter a number 1 and 100")

        else:
            if answer == guess:
                print(f"yes. it is {guess}")
                Break # If it's the right answer, stop

            elif answer > Guess: #Tell me if it's bigger or less than the answer
                print(f"It is less than {guess}")

            else: #Tell me if it's bigger or smaller than the answer
                print(f"It is less than {guess}")

    exception: # If an error occurs because an integer is not entered, output the following message:
        print ("Enter an integer")

So we've created a code for a game that matches numbers, and when you run it, you get debugging The "Enter a number bet. 1 and 100:" window appears and you enter a number "Enter an integer" is printed and only these two are repeated indefinitely. What should I fix in this case?

error try python

2022-09-20 15:52

1 Answers

input is entered as a str object.

Please make the following corrections.

guess = int(input("Enter a number bet. 1 and 100: "))


2022-09-20 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.