-4
while True:
    try:
        guess = int(input(""))

    except ValueError:
        print("You didn't enter a number!")
        
    if guess == realNumber:
        False
        guesses += 1
        print("Good Job the number was",realNumber,"\nYou guessed it in",guesses,"guesses!")
        print("Okay, have a good rest of your day goodbye!")
        break

    elif guess >= 11:
        print("Only guess numbers from 1 to 10!")

    elif guess <= 0:
        print("Only guess numbers from 1 to 10!")

    elif guess < realNumber:
        print("Too Low!")
        guesses += 1

    elif guess > realNumber:
        print("Too High!")
        guesses += 1

    else:
        quit

It is broken & keeps telling me I didn't enter a number anyone know why this doesn't work?

This code is the main part of a higher or lower game I am making & I am trying to make sure you can't break it by typing letters but it has broken it more.

flakes
  • 17,121
  • 8
  • 34
  • 75
Blixtt
  • 1
  • 2
    What are you inputting? – ForceBru May 04 '21 at 17:49
  • Why is there a random `False` after `if guess == realNumber:`? – Have a nice day May 04 '21 at 17:49
  • [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/3890632) – khelwood May 04 '21 at 17:50
  • I am inputting numbers between 1 & 10. and I don't know why there is a random False I will delete that now – Blixtt May 04 '21 at 17:52
  • Your code works for me - what are you inputing (e.g. "9" or "nine"?), and where? You might consider to store your input in a variable before converting to int. that way you could add your input to the exception message – natter1 May 04 '21 at 18:02

1 Answers1

0

Here is the working code. I added guesses and realNumber outside of the loop since they were never declared, where realNumber is the number you are trying to guess. I also stuck all the code in the try block.

guesses = 0
realNumber = 6

while True:
    try:
        guess = int(input(""))

        if guess == realNumber:
            guesses += 1
            print("Good Job the number was",realNumber,"\nYou guessed it in",guesses,"guesses!")
            print("Okay, have a good rest of your day goodbye!")
            break

        elif guess >= 11:
            print("Only guess numbers from 1 to 10!")

        elif guess <= 0:
            print("Only guess numbers from 1 to 10!")

        elif guess < realNumber:
            print("Too Low!")
            guesses += 1

        elif guess > realNumber:
            print("Too High!")
            guesses += 1

        else:
            quit

    except ValueError:
        print("You didn't enter a number!")
Have a nice day
  • 987
  • 4
  • 10
  • I don't think its a good idea to put all the code into the try ... except block. That might shadow an issue in another code line. Beside that, what did you change to make the code work? The original code seemed to work already. – natter1 May 04 '21 at 18:40
  • @natter1 Well, first of all `realNumber` and `guesses` were never defined... – Have a nice day May 04 '21 at 18:41
  • Oc thats because Blixtt is not showing the whole code (but thats more about improving his question). The fact that he gets a ValueError is suprising, because the code he has shown would not do that (same as your code). So its possible that he uses wrong input, or there is a mistake in the code part not shown (e.g. assining something to input) – natter1 May 04 '21 at 18:48