我卡住了,我不知道如何將變數傳遞給另一個模塊。
我有一個包含模塊的包:
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所有作業后
uj5u.com熱心網友回復:
您可以class variable在此處使用屬性,其中所有類實體之間共享相同的值,如果發生任何更改,它會發生在所有實體中
您可以將變數定義為類實體并使用它們
代碼結構:Game_Variables.py檔案
class Variables:
floor = 1
moves = 0
money = 0
您可以在其他類或模塊中使用這些功能
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]'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467273.html
下一篇:Rstudio推文回傳null
