Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:name';数学';没有定义错误吗?_Python - Fatal编程技术网

Python:name';数学';没有定义错误吗?

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

我是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
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