4
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?

noel pearce
  • 69
  • 1
  • 1
  • 4

7 Answers7

11

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")
Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
4

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

Arkadio
  • 55
  • 4
0

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

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0
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,229
  • 1
  • 11
  • 20
hariK
  • 2,722
  • 13
  • 18
0

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

Might be a bit too late but to do this you can do this:

from os import system
from time import sleep
while True:
    try:
        numb = float(input("Enter number>>>"))
        break

    except ValueError:
        system("cls")
        print("Error! Numbers only!")
        sleep(1)
        system("cls")

but to make it within a number range you can do this:

from os import system
from time import sleep
while True:
    try:
        numb = float(input("Enter number within 1-5>>>"))
        if numb > 5 or numb < 1:
            raise ValueError
        else:
            break

    except ValueError:
        system("cls")
        print("Error! Numbers only!")
        sleep(1)
        system("cls")
The One
  • 3
  • 2
-1

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

GLHF
  • 3,835
  • 10
  • 38
  • 83