Your third attempt is correct - but what is happening to guess_row before/after this code? For example, consider the following:
a = "Hello"
try:
a = int(input("Enter a number: "))
except ValueError:
print("Not an integer value...")
print(str(a))
If you enter a valid number, the final line will print out the value you entered. If not, an exception will be raised (showing the error message in the except block) and a will remain unchanged, so the final line will print "Hello" instead.
You can refine this so that an invalid number will prompt the user to re-enter the value:
a = None
while a is None:
try:
a = int(input("Enter a number: "))
except ValueError:
print("Not an integer value...")
print(str(a))
Answer from user4200092 on Stack OverflowYour third attempt is correct - but what is happening to guess_row before/after this code? For example, consider the following:
a = "Hello"
try:
a = int(input("Enter a number: "))
except ValueError:
print("Not an integer value...")
print(str(a))
If you enter a valid number, the final line will print out the value you entered. If not, an exception will be raised (showing the error message in the except block) and a will remain unchanged, so the final line will print "Hello" instead.
You can refine this so that an invalid number will prompt the user to re-enter the value:
a = None
while a is None:
try:
a = int(input("Enter a number: "))
except ValueError:
print("Not an integer value...")
print(str(a))
To illustrate the comments, from 3.4.2 Idle Shell on Windows, python.org (PSF) installer
>>> n = int(input('Guess1: '))
Guess1: 2
>>> n2 = float(input('Guess2: '))
Guess2: 3.1
>>> n, n2
(2, 3.1)
What system are you using and how did you install Python?
python - How can I read inputs as numbers? - Stack Overflow
How to force integer input in Python 3.x? - Stack Overflow
Is there a way to make input() accept strings and integers?
How to force an integer input
Since Python 3, input returns a string which you have to explicitly convert to int, like this
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
You can accept numbers of any base like this
>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
The second parameter tells it the base of the number and then internally it understands and converts it. If the entered data is wrong it will throw a ValueError.
>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
For values that can have a fractional component, the type would be float rather than int:
x = float(input("Enter a number:"))
Differences between Python 2 and 3
Summary
- Python 2's
inputfunction evaluated the received data, converting it to an integer implicitly (read the next section to understand the implication), but Python 3'sinputfunction does not do that anymore. - Python 2's equivalent of Python 3's
inputis theraw_inputfunction.
Python 2.x
There were two functions to get user input, called input and raw_input. The difference between them is, raw_input doesn't evaluate the data and returns as it is, in string form. But, input will evaluate whatever you entered and the result of evaluation will be returned. For example,
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
The data 5 + 17 is evaluated and the result is 22. When it evaluates the expression 5 + 17, it detects that you are adding two numbers and so the result will also be of the same int type. So, the type conversion is done for free, and 22 is returned as the result of the input and stored in the data variable. You can think of input as the raw_input composed with an eval call.
>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
Note: You should be careful when you are using input in Python 2.x. I explained why one should be careful when using it, in this answer.
But, raw_input doesn't evaluate the input and returns as it is, as a string.
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
Python 3.x
Python 3.x's input and Python 2.x's raw_input are similar and raw_input is not available in Python 3.x.
>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.
This means that, just like raw_input, input in Python 3.x always returns a string object.
To fix the problem, you need to explicitly make those inputs into integers by putting them in int:
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
I have to make a script that takes grades in a while loop and when the user is done entering grades they enter 'done'. I got the loop to continuously run but when I enter 'done' it says: "invalid literal for int() with base 10: 'done'"
I'm trying to create a password generator which can take a numerical input (a) from the user which specifies the desired password length. I'm currently using:
int(input('Enter desired password length: ')) which allows an integer value to be specified, but it returns an error if a is non-integer. Is there a way to use the while loop to bypass this?