从导入的文件更新布尔变量
Updating a boolean variable from an imported file
我是编程新手,遇到了 运行 问题。
导入函数 bonnie_movement
应该在变量 game_end
为真时停止 运行ning,现在当我尝试在它所在的文件中更新该变量(也被导入)时已导入到它不会更新它。示例:
import os
import random
import threading
import time
from movement_testing import *
def p():
global game_end
a = input("> ")
if a == "end":
game_end = True
time.sleep(0.5)
print(str(game_end))
threading.Thread(target=p).start()
threading.Thread(target=bonnie_movement).start()
你在那里看到的印刷品虽然发出了 True
但 bonnie_movement
只是继续前进。有人知道我能做什么吗?
提前致谢!
编辑:变量 a
与问题无关,它没有任何实际用途,只是在立即声明 game_end = True
时停止函数。打印也是一种查看 game_end
是否在函数 p
中更新并且服务没有实际用途的方法。
欢迎来到 Whosebug!
如果你想根据a
的值打印True
,那么你甚至不需要使用game_end
变量。事实上,您已经在条件中使用了 a
变量。
现在,由于我不知道 movement_testing
class 中的 game_end
的真正作用,我将尝试使用您已经提供的信息来指导您。您可以简单地修改 def p()
函数,如下所示:
def p():
if a == "end":
print(True)
甚至
def p():
print(a=="end")
如果您可以在此文件中声明 game_end
变量或在 movement_testing
.
中定义 def p()
方法,则更好的方法是
祝你一路顺风!
我在 reddit 上发布了同样的问题,这是答案:
Globals in python are global to modules, not across all modules. The line you claim imports the variable actually doesn't.
If 'game_end' is a property of 'Movement_testing' then try accessing it like
movement_testing.game_end = True
and
print(str(movement_testing.game_end))
我是编程新手,遇到了 运行 问题。
导入函数 bonnie_movement
应该在变量 game_end
为真时停止 运行ning,现在当我尝试在它所在的文件中更新该变量(也被导入)时已导入到它不会更新它。示例:
import os
import random
import threading
import time
from movement_testing import *
def p():
global game_end
a = input("> ")
if a == "end":
game_end = True
time.sleep(0.5)
print(str(game_end))
threading.Thread(target=p).start()
threading.Thread(target=bonnie_movement).start()
你在那里看到的印刷品虽然发出了 True
但 bonnie_movement
只是继续前进。有人知道我能做什么吗?
提前致谢!
编辑:变量 a
与问题无关,它没有任何实际用途,只是在立即声明 game_end = True
时停止函数。打印也是一种查看 game_end
是否在函数 p
中更新并且服务没有实际用途的方法。
欢迎来到 Whosebug!
如果你想根据a
的值打印True
,那么你甚至不需要使用game_end
变量。事实上,您已经在条件中使用了 a
变量。
现在,由于我不知道 movement_testing
class 中的 game_end
的真正作用,我将尝试使用您已经提供的信息来指导您。您可以简单地修改 def p()
函数,如下所示:
def p():
if a == "end":
print(True)
甚至
def p():
print(a=="end")
如果您可以在此文件中声明 game_end
变量或在 movement_testing
.
def p()
方法,则更好的方法是
祝你一路顺风!
我在 reddit 上发布了同样的问题,这是答案:
Globals in python are global to modules, not across all modules. The line you claim imports the variable actually doesn't.
If 'game_end' is a property of 'Movement_testing' then try accessing it like
movement_testing.game_end = True
and
print(str(movement_testing.game_end))