
So, you may want to consider moving the validation out to a separate function from the looping over input, to make it more readable.
#Python convert string to integer code
Obviously, the more validation you want, the more code it takes, because each test is another line of code. For example, what if they type -23 or 178? Those are integers, but they're not numbers between 1 and 100. You may also want some other checks even inside the try part. Or, if you want to truncate floats, change that int(userGuess) to int(float(userGuess)). 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. 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. Print(type(userGuess), "after int failed") Print(type(userGuess), "after int succeeeded") 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(): That means you're still going to treat, say, "Hello!" as a number, because it has at least one non-letter. userGuess.isalpha() only tells you that the guess is made entirely of letters. Second, the way you're approaching this is wrong. ValueError: invalid literal for int() with base 10: '4.3'įirst, 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…?

Traceback (most recent call last):įile "C:\\*******.py\line 28, in validateInput Here's the error I'm getting if I use 4.3 (or any float) as input. Print(type(userGuess), "at 'isalpha() = True'") Print("Please enter whole numbers only, no words.") Print(type(userGuess), "at 'isalpha() = False'") Here’s an example: stringvalue '42' integervalue int (stringvalue) print (integervalue) Output: 42 This converts the stringvalue 42 to the integer value 42.

UserGuess = input("Please enter a number from 1 to 100. In Python, you can convert a string to an integer using the int () function. Here's the function from my main program. As I said I'm on about day 3 of this coding thing so try to be understanding of my little knowledge.

I can't get it to convert the float number over to an integer. I'm having trouble when I put in a float number.
#Python convert string to integer how to
I've figured out how to weed out alphabetic characters, so I can convert the numbers into an integer. The syntax of the method, int in python is very simple: int(value, base) The value parameter is required but the base parameter is optional. The int () method takes a string or integer data type and converts the given data into an integer number. So that only integers are accepted as input. We can convert string to int using the python built-in method called the int () method. Just trying to write a simple number guess game, but also do input validation. I'm new to the whole coding thing.so here goes.
