2014-10-05 71 views
-2

main.py:从另一个模块中使用变量王氏while循环

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:

def true_the_another_loop(): 
    loop = False 
    anotherLoop = True 

def print_hello(): 
    print "hello" 

def print_world(): 
    print "world" 

当我运行main.py,只会打印"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

您不需要将返回值放入列表中 – 2014-10-05 19:48:21