2013-10-12 53 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