Python:name';数学';没有定义错误吗?
我是python初学者,无法理解为什么会发生这种情况:Python:name';数学';没有定义错误吗?,python,Python,我是python初学者,无法理解为什么会发生这种情况: from math import * print "enter the number" n=int(raw_input()) d=2 s=0 while d<n : if n%d==0: x=math.log(d) s=s+x print d d=d+1 print s,n,float(n)/s 从数学导入* 打印“输入号码” n=int(原始输入() d=2 s=0
from math import *
print "enter the number"
n=int(raw_input())
d=2
s=0
while d<n :
if n%d==0:
x=math.log(d)
s=s+x
print d
d=d+1
print s,n,float(n)/s
从数学导入*
打印“输入号码”
n=int(原始输入()
d=2
s=0
d0.05
到
从X导入使用*
通常不是一个好主意,因为它会无法控制地污染全局命名空间,并可能带来其他困难。您需要导入数学
,而不是从数学导入中导入*
您犯了一个错误
当你写道:
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
因此,当您尝试使用math.log
它会给您带来错误,因此:
将math.log
替换为log
或
将数学导入中的替换为数学导入
这应该可以解决问题。如果您只需要math.pi
),那么:
然后像PI
symbol?哦,我实际上是在用麻省理工学院开放式课程学习,在他们的习题集中,它是用数学导入编写的*@Danny:我不知道他们为什么选择那样写,但我建议不要用从X导入*
@Danny:在某些情况下,使用*
导入是合理的,但正如@aix所说,这“通常不是一个好主意”。因此,对于简单的脚本,您不会进行扩展,您可能不关心,但对于更复杂的脚本,您应该关心。还有哪些困难?@MattFenwick:主要是无法重新加载()以这种方式导入的内容。关于模块:
from math import *
import math
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
from math import pi as PI