如何在具有功能的两个模块之间传递变量?
How to passing variables between two modules which have a function?
我卡住了,我不知道如何将变量传递给另一个模块。
我有一个包含模块的包:
Game_With_Function_Map.py
Game_informations.py
Game_Variables.py
在Game_Variables.py
中我有一个变量:
floor = 1
moves = 0
moneys = 0
在Game_informations.py
中:
from game_variable import player_chests, money, floor, moves,
def informations(info):
if info == "info":
sorted(player_chests.items(), key=lambda x: x[1], reverse=True)
print(
f'\n[Money {money}],\n[Floor {floor}],\n[moves {moves}],\n[Ekwipunek]')
并且在主文件中 Game_With_Function_Map.py
:
from Game_informations import informations as game_info
from game_variable import player_chests as player_chest
from game_variable import money
这段代码,改一个值在game_variable
else:
global money
player_chest[chest_color] -= 1
add_gold = draw_money(
chance_on_money[gold_in_chest[f'{chest_color}']])
money = money + add_gold
print('Added', add_gold, 'gold')
将变量移动到 game_variable.py
后一切正常
您可以在此处使用 class variable
属性,其中所有 classes 实例共享相同的值,如果发生任何变化,所有实例都会发生变化
您可以将变量定义为 class 实例并按原样使用它们
代码结构:Game_Variables.py
文件
class Variables:
floor = 1
moves = 0
money = 0
并且您可以在其他 classes 或模块中使用这些函数,如
from Game_Variables import Variables
from game_variable import player_chests, money, floor, moves,
def informations(info):
if info == "info":
sorted(player_chests.items(), key=lambda x: x[1], reverse=True)
print(
f'\n[Money {Variables.money}],\n[Floor {Variables.floor}],\n[moves {Variables.moves}],\n[Ekwipunek]'
我卡住了,我不知道如何将变量传递给另一个模块。
我有一个包含模块的包:
Game_With_Function_Map.py
Game_informations.py
Game_Variables.py
在Game_Variables.py
中我有一个变量:
floor = 1
moves = 0
moneys = 0
在Game_informations.py
中:
from game_variable import player_chests, money, floor, moves,
def informations(info):
if info == "info":
sorted(player_chests.items(), key=lambda x: x[1], reverse=True)
print(
f'\n[Money {money}],\n[Floor {floor}],\n[moves {moves}],\n[Ekwipunek]')
并且在主文件中 Game_With_Function_Map.py
:
from Game_informations import informations as game_info
from game_variable import player_chests as player_chest
from game_variable import money
这段代码,改一个值在game_variable
else:
global money
player_chest[chest_color] -= 1
add_gold = draw_money(
chance_on_money[gold_in_chest[f'{chest_color}']])
money = money + add_gold
print('Added', add_gold, 'gold')
将变量移动到 game_variable.py
后一切正常
您可以在此处使用 class variable
属性,其中所有 classes 实例共享相同的值,如果发生任何变化,所有实例都会发生变化
您可以将变量定义为 class 实例并按原样使用它们
代码结构:Game_Variables.py
文件
class Variables:
floor = 1
moves = 0
money = 0
并且您可以在其他 classes 或模块中使用这些函数,如
from Game_Variables import Variables
from game_variable import player_chests, money, floor, moves,
def informations(info):
if info == "info":
sorted(player_chests.items(), key=lambda x: x[1], reverse=True)
print(
f'\n[Money {Variables.money}],\n[Floor {Variables.floor}],\n[moves {Variables.moves}],\n[Ekwipunek]'