2016-09-08 75 views
0

這裏是我的代碼閱讀數學函數問題

import math 

try: 

    valor = float(input("Give a real number ")) 
    print("Your value given is: ", value) 
except ValueError: 

    print("You gave a value not interpretable as a real onel!!") 

而當我輸入爲sqrt(2),我得到這個錯誤,任何人都知道這是爲什麼?

%run "c:\users\aar15\appdata\local\temp\tmpvzauzz.py" 

Give a real number sqrt(2) 
--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 

c:\users\aar15\appdata\local\temp\tmpvzauzz.py in <module>() 

     1 import math 
     2 try: 
----> 3  valor = float(input("Give a real number ")) 

     4  print("Your value given is: ", value) 
     5 except ValueError: 

C:\Users\aar15\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.1.3253.win-x86_64\lib\site-packages\IPython\kernel\zmq\ipkernel.pyc in <lambda>(prompt) 

C:\Users\aar15\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.1.3253.win-x86_64\lib\site-packages\IPython\kernel\zmq\ipkernel.pyc in <module>() 

NameError: name 'sqrt' is not defined 
+1

也許開方(2)被解釋爲需要的eval一個字符串()在鑄造浮動之前?或者你需要math.sqrt(2)。測試valor = float(sqrt(2)))是否給出錯誤可能會很好。 –

回答

0

你可以從doc看到,input相當於eval(raw_input(prompt)),但你仍然可以讓它工作:

>>> import math 
>>> input() 
math.sqrt(2) 
1.4142135623730951 
>>> from math import * 
>>> input() 
sqrt(2) 
1.4142135623730951