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 Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-take-integer-input-in-python
How to take integer input in Python? - GeeksforGeeks
April 3, 2026 - To take integer input, the string must be typecast into an integer using the int() function. This article demonstrates multiple ways to take integer input in Python with examples.
Discussions

python - How can I read inputs as numbers? - Stack Overflow
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: More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to force integer input in Python 3.x? - Stack Overflow
I'm trying to make a program in Python that takes in an input for how many times to repeat the Fibonacci sequence. ... i=1 timeNum= input("How many times do you want to repeat the sequence?") whi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a way to make input() accept strings and integers?
Instead of casting input straight into an integer you can wait and check if it's done first. Instead of: grade = int(input("Enter grade: ")) Do this: grade = input("Enter grade: ") if grade.lower() == 'done': print("Done.") else: integer_grade = int(grade) More on reddit.com
๐ŸŒ r/learnpython
6
3
October 8, 2021
How to force an integer input
Use a try to catch the error. You can put the try in a while loop to repeatedly ask until an error isn't thrown. More on reddit.com
๐ŸŒ r/learnpython
6
1
March 7, 2022
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ python tutorial: how to take an integer input in python
Python Tutorial: How to take an integer input in Python - Pierian Training
April 12, 2023 - In Python, we can take input from the user using the `input()` function. The `input()` function reads a line of text entered by the user and returns it as a string. To take an integer input, we need to convert the string input to an integer.
๐ŸŒ
ScriptVerse
scriptverse.dev โ€บ tutorials โ€บ python-program-integer-input.html
Read/Take Number/Integer Input in Python 3 - ScriptVerse
If you require only integer inputs, you need to type-cast the input into an integer. For this, we make use of the built-in function int() which returns an integer object to the passed argument.
๐ŸŒ
Real Python
realpython.com โ€บ python-input-integer
How to Read Python Input as Integers โ€“ Real Python
March 18, 2026 - In Python 3, the input() function returns a string, so you need to convert it to an integer.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ how to take integer input in python 3 | example code
How to take integer input in Python 3 | Example code
May 23, 2023 - In a simple example code taking integer input, we have to typecast those inputs into integers by using Pythonโ€™s built-in int() function. age = int(input("Enter age: ")) print(age) print(type(age)) ... Keep in mind that if the user enters a ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-take-integer-input-in-python
How to take integer input in Python?
August 28, 2023 - Use int(input()) for single integers and map(int, input().split()) for multiple integers. Always include error handling for robust programs that gracefully handle invalid input. --- Tushar Sharma ยท Updated on: 2026-03-27T13:59:25+05:30 ยท 3K+ ...
Find elsewhere
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ input-and-input-in-python
Python input() Function: Get User Input with Examples
June 5, 2026 - For instance if we aim to treat the input as an integer we can utilize the int() function for conversion. Ensuring that the input is converted into the data type is fundamental, for ensuring smooth operation of the program. This practice helps in preventing errors and facilitates proper ...
Top answer
1 of 10
407

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 input function evaluated the received data, converting it to an integer implicitly (read the next section to understand the implication), but Python 3's input function does not do that anymore.
  • Python 2's equivalent of Python 3's input is the raw_input function.

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'>)
2 of 10
54

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: "))
๐ŸŒ
NBShare
nbshare.io โ€บ notebook โ€บ 956257822 โ€บ How-To-Take-Integer-Input-From-Command-Line-In-Python
How To Take Integer Input From Command Line In Python
To fix this, we will have to convert the input to integer before assigning to a variable. ... Similarly we can tweak our previous code to take a Floating point number as input. ... Similarly we can use above code snippets in Python 3 by replacing the Python input function raw_input() with input().
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ how to take integer input in python
How to Take Integer Input in Python - Java2Blog
November 29, 2023 - The int() function then attempts to convert this string to an integer. If the conversion is unsuccessful (e.g., if the user enters a non-integer value), a ValueError is raised, which we handle using a try-except block. This method is simple and effective for most use cases.
๐ŸŒ
Medium
learningdaily.dev โ€บ how-to-take-integer-input-in-python-09decb2b129e
How to take integer input in Python | by The Educative Team | Dev Learning Daily
May 21, 2026 - This method is highly effective in scenarios requiring multiple integer inputs, as it ensures each input is valid before proceeding to the next, therefore maintaining the integrity and accuracy of the data collected. ... This approach may be used in applications like surveys and data entry systems, where precise and valid numerical inputs are required. num_inputs = 3 numbers = [] output = 1 for _ in range(num_inputs): try: number = int(input()) numbers.append(number) except ValueError: print("Please enter valid integer inputs.") output = 0 if output: print("You entered the integers:", numbers)
๐ŸŒ
py4u
py4u.org โ€บ blog โ€บ how-to-take-integer-input-in-python
How to Take Integer Input in Python?
In Python, the input() function is used to take user input from the standard input (usually the keyboard). By default, it returns the input as a string. To get an integer, you need to convert the input string to an integer using the int() function.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ read-input-as-an-integer.aspx
How to take integer input in Python?
There is no such method, that can be used to take input as an integer directly โ€“ but input string can be converted into an integer by using int() function which accepts a string and returns an integer value. Thus, we use input() function to read input and convert it into an integer using ...
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ how-to-take-integer-input-in-python
How to take integer input in Python
The input() function in Python is designed to accept user input, returning whatever the user types as a string. This necessitates the conversion of this string to an integer when numerical input is needed.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python input: take input from user
Python Input(): Take Input From User [Guide]
February 23, 2024 - Enter roll number 22 Enter age Jessa Roll number: 22 Name: Jessa Printing type of a input values type of number <class 'str'> type of name <class 'str'> As you know whatever you enter as input, the input() function always converts it into a string.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to force an integer input
r/learnpython on Reddit: How to force an integer input
March 7, 2022 -

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?

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1133138 โ€บ i-want-to-take-integer-input-in-python-within-a-range-how-do-i-do-that
I want to take integer input in python within a range how do ...
March 10, 2018 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.