How to make input only accept numbers python

mark= eval(raw_input("What is your mark?"))
try:
    int(mark)
except ValueError:
    try:
        float(mark)
    except ValueError:
        print "This is not a number"

So I need to make a python program that looks at your mark and gives you varying responses depending on what it is.

However I also need to add a way to stop random text which isn't numbers from being entered into the program.

I thought I had found a solution to this but it won't make it it past the first statement to the failsafe code that is meant to catch it if it was anything but numbers.

So pretty much what happens is if I enter hello instead of a number it fails at the first line and gives me back an error that says exceptions:NameError: name 'happy' is not defined.

How can I change it so that it can make it to the code that gives them the print statement that they need to enter a number?

asked Dec 16, 2014 at 23:51

How to make input only accept numbers python

5

remove eval and your code is correct:

mark = raw_input("What is your mark?")
try:
    int(mark)
except ValueError:
    try:
        float(mark)
    except ValueError:
        print("This is not a number")

Just checking for a float will work fine:

try:
    float(mark)
except ValueError:
    print("This is not a number")

answered Dec 16, 2014 at 23:55

How to make input only accept numbers python

1

Is it easier to declare a global value than to pass an argument, In my case it's also gives an error.

def getInput():
    global value
    value = input()
    while not value.isnumeric():
        print("enter a number")
        value = input("enter again")
    return int(value)

getInput()
print(value)

#can't comment :)

answered Feb 28, 2021 at 10:26

You can simply cae to float or int and catch the exception (if any). Youre using eval which is considered poor and you add a lot of redundant statements.

try:
    mark= float(raw_input("What is your mark?"))
except ValueError:
    print "This is not a number"

"Why not use eval?" you ask, well... Try this input from the user: [1 for i in range (100000000)]

answered Dec 16, 2014 at 23:55

How to make input only accept numbers python

Reut SharabaniReut Sharabani

29.5k6 gold badges69 silver badges86 bronze badges

you can use the String object method called isnumeric. it's more efficient than try- except method. see the below code.

def getInput(prompt):
    value = input(prompt)
    while not value.isnumeric():
        print("enter a number")
        value = input("enter again")
    return int(value)

answered Apr 5, 2019 at 8:20

import re

pattern = re.compile("^[0-9][0-9]\*\\.?[0-9]*")

status = re.search(pattern, raw_input("Enter the Mark : "))

if not status:

        print "Invalid Input"

Abdul Hadi

1,1991 gold badge10 silver badges19 bronze badges

answered Dec 17, 2014 at 1:25

How to make input only accept numbers python

hariKhariK

2,42211 silver badges16 bronze badges

Actually if you going to use eval() you have to define more things.

acceptables=[1,2,3,4,5,6,7,8,9,0,"+","*","/","-"]
try:
    mark= eval(int(raw_input("What is your mark?")))
except ValueError:
    print ("It's not a number!")
if mark not in acceptables:
    print ("You cant do anything but arithmetical operations!")

It's a basically control mechanism for eval().

answered Dec 16, 2014 at 23:59

How to make input only accept numbers python

GLHFGLHF

3,71410 gold badges35 silver badges80 bronze badges

How do you accept numbers in Python?

Use Python built-in input() function to take integer input from the user. This input() function returns string data and it can be stored in a string variable. Then use the int() function to parse into an integer value.

How do you accept 10 numbers in Python?

Let's see how to accept Python list as an input without using the split() method..
First, create an empty list..
Next, accept a list size from the user (i.e., the number of elements in a list).
Run loop till the size of a list using a for loop and range() function..
use the input() function to receive a number from a user..