Question

I am having trouble with the eval() Python function in this code:

 f = 'x'
 f = raw_input('f(x)=')

def ytest(y,x,f):
    if y >= eval(f)
        return 1
    else
        return 0

I know that eval() is a bad way to do this, but I am the only person who will ever be using my simple program. Whenever I try to run this I get the error:

  File "C:\Users\Grant\Desktop\Plotter\plotter.py", line 9
    if y >= eval(f)
                  ^
SyntaxError: invalid syntax

Why is this causing an error? If f is a valid python expression in string form, shouldn't the code be valid?

the error also happens when I replace eval(f) with eval('0') so I can't figure out what the problem is.

Was it helpful?

Solution

You need colon at the end of if and else statements.

if y >= eval(f):
    return 1
else:
    return 0

EDIT: Added documentation and else

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top