Python 通过while循环使用另一个模块中的变量

Python 通过while循环使用另一个模块中的变量,python,Python,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: def true_the_another_loop(): loop = False

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:

def true_the_another_loop():
    loop = False
    anotherLoop = True

def print_hello():
    print "hello"

def print_world():
    print "world"
当我运行main.py时,它只打印“hello”。为什么没有打印
“世界”


在另一个循环()中,行
loop=Flase
似乎不起作用。

您需要返回这些变量的新值。因为它们只是局部变量,所以仅限于该函数。您需要将它们的值传递给其他变量

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

您不需要将返回值放入列表中