0

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?

  • 1
    This question could be useful: [Python: Check if a string represents an int, Without using Try/Except?](http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except) – awesoon Aug 01 '16 at 15:06
  • The edit sounds like a [different problem](http://stackoverflow.com/questions/ask) entirely. – Ryan Bemrose Aug 01 '16 at 16:36
  • Looking at your edit, it's all about the order you define it. Python will try and assign a data type depending on what the value is. Your edited code is attempting to check if the value is legitimate before you actually get it from the user. Defining the variable at the start of the program will get rid of the runtime errors if done right but still won't be doing input validation. See my answer below for the correct order. You want to 1. Get the user input. 2. Check it's valid. 3. If valid perform the operations on it – Brae Aug 02 '16 at 13:04

2 Answers2

0

When the user chooses a number, his input is returned as string. Afterwards, you try to convert that string to an integer. This works fine for strings as "13" or "4", but doesn't for strings like " 3" or "2a". Therefore an exception is raised in such a case. As workaround, you can check the string with the "isdigit()" method before converting it. That method will return True for strings containing only digits and False otherwise.

Rafael Albert
  • 445
  • 2
  • 8
0

Your issue is that the line userGuess = int(input("I guess: ")) is converting to an integer value without checking if it's possible to do so. When it finds a character value, this will throw an exception since the conversion isn't possible.

You could prevent this with a try...catch but I think a better way is to use the isDigit() method to check if the value is only digits before you try and convert.

while guessNum < 3:
    userGuess = input("I guess: ")
    if (userGuess.isDigit()):
        userGuess = int(userGuess)

        if userGuess > randNum:
            print("Too high!  Try again.")
            guessNum = guessNum + 1

        ...

        if userGuess == randNum:
            print("Great!  You guessed my number!")
            break
    else:
        print("Please choose a valid answer.")
Brae
  • 484
  • 1
  • 3
  • 15