2014-10-05 3 views
-2

는 main.py :사용 변수

import sys 
sys.path.append('Pygame Projects') 
import sub 
from sub import * 

loop = True 

while loop: 
    print_hello() 
    true_the_another_loop() 

while anotherLoop: 
    print_world() 

sub.py는 : 내가 main.py를 실행하면

def true_the_another_loop(): 
    loop = False 
    anotherLoop = True 

def print_hello(): 
    print "hello" 

def print_world(): 
    print "world" 

, 그것은 단지 "hello" 인쇄합니다. "world"이 인쇄되지 않는 이유는 무엇입니까?

true_the_another_loop()에서 loop = Flase은 작동하지 않는 것 같습니다.

답변

0

이러한 변수의 새 값을 반환해야합니다. 그것들은 지역 변수이기 때문에 그 함수에만 국한되어 있습니다. 다른 변수에 값을 전달해야합니다.

... 
while loop: 
    print_hello() 
    loop, anotherLoop = true_the_another_loop() 
... 
def true_the_another_loop(): 
    loop = False 
    anotherLoop = True 
    return [loop,anotherLop] 
+1

반환 값을 목록에 넣지 않아도됩니다. –