进口麻烦
这是我的模块的一部分: gm.py
def avg_list(list):
sum = 0
for num in list:
sum += num
avg = float(sum)/len(list)
print avg
def median(list):
i = len(list)
if not i%2: # if i divided my 2 has no remainder
return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block
else:
median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s) / 2 is the middle number.
return median
当我将其保存为“gm.py”并打开一个新的脚本页面以输入以下函数时:
import gm
def stats(list):
stats = {} # empty dict named stats
stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
stats['median'] = median(list) # same for median
return stats
当我运行此程序并输入 stats([2,3,4,5, 6])...我收到一条错误消息,指出全局变量 avg_list 未定义。我不确定我的导入是否正确? 我需要做类似... from gm import avg_list() 的事情吗?
Here is part of my module: gm.py
def avg_list(list):
sum = 0
for num in list:
sum += num
avg = float(sum)/len(list)
print avg
def median(list):
i = len(list)
if not i%2: # if i divided my 2 has no remainder
return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block
else:
median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s) / 2 is the middle number.
return median
When I save this as 'gm.py' and open a new script page to input the following function:
import gm
def stats(list):
stats = {} # empty dict named stats
stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
stats['median'] = median(list) # same for median
return stats
When I run this program and type stats([2,3,4,5,6])... I get an error saying global variable avg_list not defined. I'm not sure if I'm doing the import correctly?
Do I need to do something like... from gm import avg_list() ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用模块对象上的函数:
或将函数直接导入全局命名空间:
请注意,您不应命名变量
list
。这会屏蔽内置的list()
函数/类型,如果您需要使用它,可能会在以后导致混乱的错误。您可以
按照
我认为您应该查看
median
函数的第一个分支的方式编写。列表是否也需要在那里排序,就像在第二个分支中一样?您的
avg_list
函数还屏蔽了一个内置函数sum()
,您可以在此处使用该函数,而无需手动添加:最后,查看该函数的最后一行-- 它正在
打印
avg
,但stats
期望它返回
avg
代码>.两者并不相同。Either reference the functions on the module object:
or import the functions directly into the global namespace:
Note that you should not name a variable
list
. This masks the built-inlist()
function / type and can cause confusing errors later if you need to use it.You can write
as
I think you should look at the first branch of your
median
function. Does the list need to be sorted there too, like in the second branch?Your
avg_list
function is also masking a built-in function,sum()
, which you could use here instead of manually adding:Finally, look at the last line of that function -- it's
print
ing theavg
, butstats
is expecting it toreturn
theavg
. The two aren't the same.您需要将模块名称放在前面(
gm.avg_list()
和gm.median()
),如下所示:一些参考链接和更多信息:
PEP 8 - Python 代码风格指南
Python 命名空间指南,了解
from blah import foo和<代码>导入废话
You need to put the module name first (
gm.avg_list()
andgm.median()
) like so:Some Reference links and more info:
PEP 8 - Style Guide for Python Code
A guide to Python Namespaces on the differences between
from blah import foo
andimport blah