0

I am trying to add an error when a string is entered instead of an integer. I've looked at other similar posts but when I try and implement it into my code it keeps spitting errors out. I have a number guessing game between 1 and 50 here. Can't for the life of me figure out what's wrong.

import random

number = random.randrange(1, 50)

while True:
    try:
    guess = int ( input("Guess a number between 1 and 50: ") )
    break
except ValueError:
    print("Please input a number.")**
  

while guess != number:
    if guess < number:
        print ("You need to guess higher. Try again.")
        guess = int ( input("\nGuess a number between 1 and 50: ") )
    else:
        print ("You need to guess lower. Try again.")
        guess = int ( input("\nGuess a number between 1 and 50: "))

print ("You guessed the number correctly!")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Seth Bowyer
  • 11
  • 1
  • 1
  • It would help if you shared what's the error you're getting. Your indentation is wrong so it's hard to know if that's your actual issue or a formatting problem – Tomerikoo Mar 17 '22 at 08:38
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/6045800) – Tomerikoo Mar 17 '22 at 08:40
  • The issue is mostly an indentation error as when the code is formatted correctly it works as expected – Rohith Nambiar Mar 17 '22 at 08:52
  • @RohithNambiar Not likely as the second loop will still raise an error for wrong input which doesn't sound like the expected behavior. OP is basically asking how to re-ask for input in the second loop – Tomerikoo Mar 17 '22 at 12:06

3 Answers3

2

Note that you're asking three times for the exact same input. There is really no need for that and no need for two loops at all. Just set the guess to a default value that will never be equal to the number (None) and use one single input, wrapped with try/except:

import random

number = random.randrange(1, 50)
guess = None  

while guess != number:
    try:
        guess = int(input("Guess a number between 1 and 50: "))
    except ValueError:
        print("Please input a number.")
    else:
        if guess < number:
            print("You need to guess higher. Try again.")
        elif guess > number:
            print("You need to guess lower. Try again.")

print("You guessed the number correctly!")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

You could try running a while loop for the input statements. Checking if the input(in string format) is numeric and then casting it to int. Sample code:

a = input()
while not a.isnumeric():
    a = input('Enter a valid integer')
a = int(a)

The code executes until the value of a is an int

Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19
0

your code did not work because the indentation is not right

import random

number = random.randrange(1, 50)
while True:
    try:
        guess = int ( input("Guess a number between 1 and 50: ") ) # here
        break # here
    except ValueError: # here
        print("Please input a number.")
  

while guess != number:
    if guess < number:
        print ("You need to guess higher. Try again.")
        guess = int ( input("\nGuess a number between 1 and 50: ") )
    else:
        print ("You need to guess lower. Try again.")
        guess = int ( input("\nGuess a number between 1 and 50: "))

print ("You guessed the number correctly!")

Output

Guess a number between 1 and 50: aa
Please input a number.
Guess a number between 1 and 50: 4
You need to guess higher. Try again.
Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37