2013-10-12 1 views
0

죄송합니다. 저는 파이썬 언어의 초보자입니다.이 문제는 꽤 오래 걸렸습니다. 실제로, 내림차순으로 만들고 목록을 오름차순으로 만들고 싶습니다. 내림차순과 오름차순의 모듈.하지만 나는 그것을 얻을 수 없었다. 주요 파이썬 파일이 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 

그리고이 주 파일입니다, the 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 코딩 \" 수입 selectionmodule 파일에서, 라인 1 "E : \ 코딩 \의 selectionmodule.py", 라인 1, 수입에 pythonaslab 파일 "E : \의 pythonaslab.py 코딩 \") ( selectionmodule.descendingselection에서, 라인 (16)을 AttributeError : '모듈'개체가 어떤 속성 'descendingselection' – Ball

답변

1

원형 가져 오기를 만들었습니다. pythonaslab 모듈은 selectionmodule을 가져오고 pythonaslab 모듈을 가져옵니다. 그렇게하면 불완전한 모듈로 끝납니다. 그러지 마세요.

import pythonaslab 줄을 selectionmodule에서 삭제하십시오. 해당 모듈에서 pythonaslab을 사용하고 있지 않습니다.

또한 다른 모듈은 전역을 읽을 수 없습니다. 당신은 인수로 사람들을에 전달해야합니다 : 한 문자 변수 이름에 국한되지 않습니다

selectionmodule.ascendingselection(b) 

참고 :

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

다음에 그를 호출합니다. 더 길고 설명이 포함 된 이름을 사용하면 코드를 더 쉽게 읽을 수 있습니다. 당신은 모듈 이름을 사용하지 않으려면

0

같은 :

from selectionmodule import * 

는 다음 호출 할 수 있습니다 :

ascendingselection(b) # without module name 

을 또는 당신은 할 수

selectionmodule.ascendingselection(b) 

당신은 가져와야합니다 모듈을 가져오고 별칭을 지정하십시오.

자세한 내용은3210
import selectionmodule as o 
o.ascendingselection(b) # with alias name 

읽기 : import confusaion

관련 문제