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'
Don't use
isalpha
to screen the output. EAFP -- convert it and handle that exception. Either theValueError
is exactly what you want, in that you can handle it and tell the user to correct their input. Or for some odd reason you want to silently correct their input from "4.3" to "4".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 guessed4
? Or5
? Or a legal but automatically-invalid guess? Or as actually the value4.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 atry
/except
to handle the case where it isn't: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 theexcept
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 callfloat(userGuess)
—inside theexcept
clause. Or, if you want to truncate floats, change thatint(userGuess)
toint(float(userGuess))
.You may also want some other checks even inside the
try
part. For example, what if they type-23
or178
? 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.
You could use string manipulation and typecasting.
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 tofloat
first then toint
as: