2013-10-12 51 views
0

我很抱歉,我只是一个Python语言的初学者,我很困扰这个问题很长。实际上,我想通过创建用户输入的降序和升序列表一个降序和升序的模块,但我无法得到它的工作。 主要Python文件是pythonaslab.py和上升模块和下降是selectionmodule.py..the代码:我的模块将不会加载

这是selectionmodule:

import pythonaslab 
def ascendingselection(): 
     for q in range(len(b)): 
       w=q+1 
       for w in range(len(b)): 
         if b[q]>b[w]: 
           f=b[q] 
           b[q]=b[w] 
           b[w]=f 
     print b 
def descendingselection(): 
     for q in range(len(b)): 
       w=q+1 
       for w in range(len(b)): 
         if b[q]<b[w]: 
           f=b[q] 
           b[q]=b[w] 
           b[w]=f 
     print b 

这是主文件, pythonaslab:

import selectionmodule 
a = int(input()) 
b = [int(input()) for _ in range(a)] 
print b 
print "1.ascending 2.descending" 
c=input() 
if c==1: 
     selectionmodule.ascendingselection() 
if c==2: 
     selectionmodule.descendingselection() 

你能指出我得到的所有这些错误的原因吗?

Traceback (most recent call last): 
    File "E:\Coding\pythonaslab.py", line 1, in <module> 
    import selectionmodule 
    File "E:\Coding\selectionmodule.py", line 1, in <module> 
    import pythonaslab 
    File "E:\Coding\pythonaslab.py", line 16, in <module> 
    selectionmodule.descendingselection() 
AttributeError: 'module' object has no attribute 'descendingselection' 
+2

*你得到什么*错误?你可以发布完整的追溯? –

+2

只是猜测:你的目录中是否有'__init __。py'文件? – immortal

+1

回溯(最近通话最后一个): 文件 “E:\编码\ pythonaslab.py”,1号线,在 进口selectionmodule 文件 “E:\编码\ selectionmodule.py”,1号线,在 进口pythonaslab AttributeError:'module'对象没有属性'descendingselection' – Ball

回答

1

您创建了一个循环导入;您的pythonaslab模块导入selectionmodule模块,该模块导入pythonaslab模块。你最终得到不完整的模块,不这样做。

selectionmodule中删除import pythonaslab行;您在该模块中没有使用pythonaslab

另外,另一个模块无法读取您的全局变量;你需要在传递这些作为参数:

# this function takes one argument, and locally it is known as b 
def ascendingselection(b): 
    # rest of function .. 

然后调用与:

selectionmodule.ascendingselection(b) 

请注意,您不限于一个字母的变量名。使用更长的描述性名称使您的代码更具可读性。

0

,如果你不希望使用模块的名称,如:

selectionmodule.ascendingselection(b) 

你应该导入:

from selectionmodule import * 

那么你可以拨打:

ascendingselection(b) # without module name 

或者你可以导入您的模块并指定别名:

​​

欲了解更多信息,请参阅:import confusaion