How do you input integers using input in Python

Solution 1:

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))

Solution 2:

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?

Solution 3:

However, I noticed something odd. The code works if I run it just by using the traditional run (i.e., the green button) that runs the entire code, rather than trying to execute individuals lines of code by pressing F2. Does anyone know why this may be the case?

This seems to be a problem in Eclipse, from the PyDev FAQ:

Why raw_input() / input() does not work correctly in PyDev?

The eclipse console is not an exact copy of a shell... one of the changes is that when you press in a shell, it may give you a \r, \n or \r\n as an end-line char, depending on your platform. Python does not expect this -- from the docs it says that it will remove the last \n (checked in version 2.4), but, in some platforms that will leave a \r there. This means that the raw_input() should usually be used as raw_input().replace('\r', ''), and input() should be changed for: eval(raw_input().replace('\r', '')).

Also see: PyDev 3.7.1 in Eclipse 4 — input() prepends prompt string to input variable?, Unable to provide user input in PyDev console on Eclipse with Jython.