【发布时间】:2022-09-22 21:31:38
【问题描述】:
我已经将这些全局变量从我的主模块引入到我的其他模块中,但它们不会更新。我知道这一点,因为我试图在它们应该被更新后打印变量的值。
所有变量都从\'0\'开始
这是\'module_doors\'
def one(pills, batteries, lighter):
while True:
doorone = input(\"A, B or C?:\\n\").lower()
if doorone.lower() not in (\'a\', \'b\', \'c\'):
print(\"That item doesn\'t exist, try again.\")
print(\'\')
else:
break
if doorone.lower() == \'a\':
batteries = 1
print(\'These could come in handy later.\')
if doorone.lower() == \'b\':
lighter = 1
print(\"Maybe it\'s a light source. Doesn\'t look brand new though, not sure how long it\'ll last.\")
if doorone.lower() == \'c\':
pills = 1
print(\'Could save your life, good choice.\')
然后我尝试将它们打印在另一个模块中进行检查,如下所示:
import module_doors
def lobby(pills, batteries, lighter):
if lobbydeci.lower() == \'b\':
print(\"\")
time.sleep(0.3)
print(\'Only one key remaining, Nice!\')
print(\'\')
print(\"It says \'0001\' on the tag.\")
module_spacing.spacing()
module_doors.one(pills, batteries, lighter)
module_doors.two(pills, batteries, lighter)
print(batteries)
print(lighter)
print(pills)
这只是为所有变量值打印 0,即使它们应该更新。
谢谢
-
您的代码中没有全局变量。你在说什么全局变量?
-
他们在我的主模块中:
-
作为程序员你应该非常努力,非常,非常很难不使用全局变量。
-
如:电池 = 0,打火机 = 0,药丸 = 0
-
\"他们\'在我的主模块\": 你的主模块在哪里?
标签: python