I'm new to the whole coding thing...so here goes. Just trying to write a simple number guess game, but also do input validation. So that only integers are accepted as input. I've figured out how to weed out alphabetic characters, so I can convert the numbers into an integer. I'm having trouble when I put in a float number. I can't get it to convert the float number over to an integer. Any help is appreciated. As I said I'm on about day 3 of this coding thing so try to be understanding of my little knowledge. Thanks in advance.
Here's the function from my main program.
def validateInput(): while True: global userGuess userGuess = input("Please enter a number from 1 to 100. ") if userGuess.isalpha() == False: userGuess = int(userGuess) print(type(userGuess), "at 'isalpha() == False'") break elif userGuess.isalpha() == True: print("Please enter whole numbers only, no words.") print(type(userGuess), "at 'isalpha() == True'") return userGuess
Here's the error I'm getting if I use 4.3 (or any float) as input.
Traceback (most recent call last): File "C:\\*******.py\line 58, in <module> validateInput() File "C:\\*******.py\line 28, in validateInput userGuess = int(userGuess) ValueError: invalid literal for int() with base 10: '4.3'
Actually int()
function expects an integer string or a float, but not a float string. If a float string is given you need to convert it to float
first then to int
as:
int(float(userGuess))
First, why do you want to convert the float string to an integer? Do you want to treat 4.7
as meaning the user has guessed 4
? Or 5
? Or a legal but automatically-invalid guess? Or as actually the value 4.7
(in which case you don't want integers at all)? Or…?
Second, the way you're approaching this is wrong. userGuess.isalpha()
only tells you that the guess is made entirely of letters. That means you're still going to treat, say, "Hello!"
as a number, because it has at least one non-letter.
If you want to know if a string is a valid integer, just call int
on it, and use a try
/except
to handle the case where it isn't:
def validateInput(): while True: global userGuess userGuess = input("Please enter a number from 1 to 100. ") try: userGuess = int(userGuess) print(type(userGuess), "after int succeeeded") break except ValueError: print("Please enter whole numbers only, no words.") print(type(userGuess), "after int failed") return userGuess
If you want to handle actual words differently from other kinds of failure, e.g., so you can print a more specific error message, then you can check isalpha
inside the except
clause.
If you want to handle check whether it's a float so you can give a different error, do the same thing—try
to call float(userGuess)
—inside the except
clause. Or, if you want to truncate floats, change that int(userGuess)
to int(float(userGuess))
.
You may also want some other checks even inside the try
part. For example, what if they type -23
or 178
? Those are integers, but they're not numbers between 1 and 100.
Obviously, the more validation you want, the more code it takes, because each test is another line of code. So, you may want to consider moving the validation out to a separate function from the looping over input, to make it more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With