简体   繁体   中英

why eval function is not recognizing trigonometric when I am using local variable in it?

Here is code

equation1 = input("Please Enter an equation")
a = float(input("For a = "))
c = eval(equation1)

When I am typing equation with cos, tan, sin and ln functions; ie: a*cos(a)

I am getting this error:

    c = eval(equation1)
  File "<string>", line 1, in <module>
TypeError: cos() takes exactly one argument (0 given)

and inputs like a*cos(90), a**2+cos(90) are working fine, my application is build to be generic. user can give any value is there any way to fix it?

First it needs to have from math import cos .

When I run with PyCharm, I get the same exception as you.

Then I added print(equation1) and found that when I typed cos(a) , printed cos() .

At first I thought there was something wrong with the input function, but I couldn't find the reason.

Then I ran it directly from the terminal command line using python xx.py and it worked fine.

So it may be that the PyCharm window does not recognize the contents of the parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM