python - Returning error when user inputs space into program -


this question has answer here:

like many before me, brand new @ this, go easy if haven't given information needed, , thank in advance.

before start it's worth mentioning program running fine, i'm worried making sure i've thought of every possibly scenario. here goes.

i'm receiving error:

file "c:\users\brand\desktop\wip programs\guess number 31.july.py",         line 15, in <module> userguess = int(input("i guess: ")) valueerror: invalid literal int() base 10: ' ' 

when press space bar input, returns this. not sure how make program returns useful, such ability guess again. here code, reference:

import random  guessnum = 0  print("welcome guess number game!  please, tell me name!") user = input("my name is: ")  randnum = random.randrange(1, 10, 1) #generates number  print("okay, " + user + ", guess random number, range 1 10.")  #guessing phase while guessnum < 3:     userguess = int(input("i guess: "))      if userguess > randnum:         print("too high!  try again.")         guessnum = guessnum + 1      if userguess < randnum:         print("too low!  try again.")         guessnum = guessnum + 1      if userguess == randnum:         print("great!  guessed number!")         break     else:         print("please choose valid answer.")   if userguess == randnum:     print("if play again, please restart program.")  if userguess != randnum:     print("nope.  number was: " + str(randnum)) 

if have unneeded or lacking should have, please feel free correct me!

edit!

going off of first reply. added .isdigit() code properly:

if (userguess.isdigit()):      userguess = input("i guess: ")      if userguess > randnum:         print("too high!  try again.")         guessnum = guessnum + 1 

it keeps passing exception saying 'userguess' not defined. fine! okay, define in beginning of code next user. upon running, returns

attributeerror: 'int' object has no attribute 'isdigit' 

also fine, add str(0) userguess attempt fix returns:

typeerror: unorderable types: str() > int() 

it lets me input number, cannot figure out how fix. advice?

when user chooses number, input returned string. afterwards, try convert string integer. works fine strings "13" or "4", doesn't strings " 3" or "2a". therefore exception raised in such case. workaround, can check string "isdigit()" method before converting it. method return true strings containing digits , false otherwise.


Comments