Python3 read numeric user input

In python 2.x versions, there were two functions for taking user inputs.


If you use python 2.x versions, basically you can use input() function to read numeric data.

And you can use raw_input() to read non numeric data

But if you use python 3.x versions, there are no two functions, there is only input() function available.


input() function in python 3.x is equal to raw_input() function in python 2.x versions. So it will read all the inputs as strings.

Therefore you need to convert input to number.

myNumber = int(input())

But what will happen if user enters an non numeric value? You will get an error. So you may need to check input before converting.

while true:
    myNumber = input()
    if myNumber.isnumeric():
        myNumber = int(myNumber)
        print("Please input a number")

Leave a Reply

(*) Required, Your email will not be published