Python : name 'math' is not defined Error?

前端 未结 4 2080
Happy的楠姐
Happy的楠姐 2021-01-01 09:29

I am a beginner in python and cant understand why this is happening:

from math import *
print \"enter the number\"
n=int(raw_input())
d=2
s=0
while d

        
相关标签:
4条回答
  • 2021-01-01 09:53

    You need to import math rather than from math import *.

    0 讨论(0)
  • 2021-01-01 09:56

    Change

    from math import *
    

    to

    import math
    

    Using from X import * is generally not a good idea as it uncontrollably pollutes the global namespace and could present other difficulties.

    0 讨论(0)
  • 2021-01-01 09:57

    You did a mistake..

    When you wrote :

    from math import *
    # This imports all the functions and the classes from math
    # log method is also imported.
    # But there is nothing defined with name math
    

    So, When you try using math.log

    It gives you error, so :

    replace math.log with log

    Or

    replace from math import * with import math

    This should solve the problem.

    0 讨论(0)
  • 2021-01-01 09:59

    How about (when you need only math.pi):

    from math import pi as PI
    

    and then use it like PI symbol?

    0 讨论(0)
提交回复
热议问题