Int and string types

I’m working on a personal project and I’m getting user from input that should be and int.
problem is like this:
say I get an input from a user using this code:

guess=int(input('what is your guess?:'))

and the user inserts a letter just to be annoying :stuck_out_tongue_winking_eye:
I’m gonna be getting a syntax error cause the program can’t change the letter to an integer
but if I do this

guess=input('what is your guess?:')

without the int() command all input I get will be treated as a string and not as an int like I want it to
what do I do now?

full code if needed:

import random 
number=random.randint(1,10)

guess=int(input('what is your guess?:'))

if guess not in range(1,10) or guess == 0:
  print('your guess should be a number between 1 and 10')

while guess is not number:
 guess=int(input('guess again:'))

 if guess not in range (1,10) or guess == 0:
   print('your guess should be a number between 1 and 10')

 if guess == number:
  print('great job you guessed correctly!')

There are methods enabling use to check if the value is numeric:

https://www.includehelp.com/python/difference-between-string-isdecimal-isdigit-isnumeric-and-methods.aspx

you could also handle the exception.

I would personally opt for handling the exception. Then you are absolute the conversion was successful (or unsuccessful)

hi @stetim94 thanks for the advice!
can you be a bit more specific about this? I’m not sure I understood what you meant.
thanks a lot!

Specific about what?

When exceptions occur, we can also handle/catch the exception to keep the programming running and try to prompt the user again

how do I get the program to check if its a number or not?
I can’t get to the link.

isdecimal(), isdigit(), isnumeric() are all methods which we can use to determine if the string only contains decimal, digit or numeric characters

why can’t you get to the link?

my compute is blocked with parent link.
why is this code not working?

if guess.isnumeric():
  guess=int(guess)

I’m still getting strings instead of numbers

i don’t see the problem:

image

inputting this

8

gives me this

if guess.isnumeric():
  guess=int(guess)

this should be happening only in this case:

if guess not in range (1,10) or guess == 0:
   print('your guess should be a number between 1 and 10')

can i see your full code?

import random
number=random.randint(1,10)
guess=int(input('what is your guess?:'))

if guess not in range(1,10) or guess ==0 and guess.isnumeric():
  print('your guess should be a number between 1 and 10')

while guess is not number:
 guess=input('guess again:')
 
 if guess not in range (1,10) or guess == 0:
   print('your guess should be a number between 1 and 10')

 if guess == number:
  print('great job you guessed correctly!')

you should really break this down into three steps:

first verify the number is numeric
convert the number to integer
now that you have an integer, you can verify the range

thanks @stetim94 !
problem solved!

I now told you the answer, but i would advise/recommend to reflect how/why you didn’t get to these steps yourself to learn from it :slight_smile:

K.O. commander! :stuck_out_tongue_winking_eye:

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.