I have a number game but if i type letters it breaks

Question:
help

Current behavior:
it breaks

Desired behavior
i want it to just reask the question

Repl link:

code snippet

Hey, @ChristosSteele welcome to the forums!

Can you please provide a link to the repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

Hi @ChristosSteele !
If let’s say you are doing this:

number = int(input('Enter 1 or 2: ')) # Only accepts numbers because of int()
if number == 1:
  print('You chose 1!')
elif number == 2:
  print('You chose 2!')
else:
  print('Choose 1 or 2!')

The input will break becaue ot only accepts numbers, so strings will cause an error.
To undo this, try this:

number = str(input('Enter 1 or 2: ')) # Cconverts the answer to a string, no matter the input.
if number == '1': # Checks a string, not a number
  print('You chose 1!')
elif number == '2': # Checks a string, not a number
  print('You chose 2!')
else:
  print('That was not a number')

Hope this helps!

you can also use try and except to do a similar output
example:

try:
  number = int(input("Enter 1 or 2."))
   if number == 1:
    print('You chose 1!')
  elif number == 2:
    print('You chose 2!')
  else:
    print('Please only input 1 or 2!')
except ValueError:
  print('Please only enter numbers!')
1 Like