Like many before me, I am brand new at this, so go easy if I haven't given all the information needed, and thank you in advance.
Before I start it's worth mentioning that the program itself is running fine, what i'm worried about is making sure I've thought of every possibly scenario. Here goes.
I'm receiving this error:
File "C:\Users\brand\Desktop\WIP Programs\Guess the number 31.July.py", line 15, in <module>
userGuess = int(input("I guess: "))
ValueError: invalid literal for int() with base 10: ' '
When I press the space bar for input, it returns this. I am not sure how to make it so that the program returns something useful, such as the ability to guess again. Here is my code, for reference:
import random
guessNum = 0
print("Welcome to the guess the number game! Please, tell me your name!")
user = input("My name is: ")
randNum = random.randrange(1, 10, 1) #Generates number
print("Okay, " + user + ", guess the random number, the range is 1 to 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! You guessed my number!")
break
else:
print("Please choose a valid answer.")
if userGuess == randNum:
print("If you would like to play again, please restart the program.")
if userGuess != randNum:
print("Nope. My number was: " + str(randNum))
If I have any unneeded or am lacking anything I should have, please feel free to correct me!
EDIT!
Going off of the first reply. I added .isdigit() into my code properly:
if (userGuess.isdigit()):
userGuess = input("I guess: ")
if userGuess > randNum:
print("Too high! Try again.")
guessNum = guessNum + 1
It keeps passing an exception saying that 'userGuess' is not defined. Fine! okay, So I define it in the beginning of my code next to user. Which upon running, returns
AttributeError: 'int' object has no attribute 'isdigit'
Also fine, so I add str(0) to userGuess to attempt a fix which then returns:
TypeError: unorderable types: str() > int()
It now lets me input a number, however I cannot figure out how to fix. Any advice?