我正在嘗試使用另一個檔案中的函式為某些變數生成隨機值,但它似乎不起作用
功能
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
使用函式的代碼(函式所在的檔案名為 monsterstats.py)
from monsterstats import *
common_monsterstat()
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
uj5u.com熱心網友回復:
你的范圍有問題。創建函式時,您為其賦值的每個變數都是區域變數(除非您使用global關鍵字)。您可以像這樣使用全域關鍵字:
mhealth=0
def common_monsterstat():
global mhealth
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = health
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
common_monsterstat()
這將使您的代碼更改全域運行狀況,而不是創建它的副本...
但更好的方法是簡單地回傳健康變數,因為根據我的經驗,全域關鍵字可能會在一段時間后變得混亂。但是,如果您需要您的游戲真正高效,而您可能不需要,使用 global 可以避免創建太多變數(雖然不確定它是否會更高效,我只是這么認為 :))所以,您可以像這樣做:
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return mhealth
def battlesys():
mhealth=common_monsterstat()
print("You challenged Monster!")
print("Monster's health: " mhealth)
common_monsterstat()
battlesys()
這將創建一個local變數,為其賦值,將其回傳到另一個變數中,然后列印它......看起來更復雜但實際上更容易......
如果您需要任何澄清,請告訴我!PS:你可能想為你的怪物起一個隨機的名字,并用一個 f-string 以更酷的方式命名:
monsters=['centaurus','dwarf','spider']
def battlesys():
print(f"You challenged {r.choice(monsters)}!")
print("Monster's health: " health)
PPS:您沒有在代碼中匯入隨機...
uj5u.com熱心網友回復:
在def common_monsterstat()函式內部為 mmaxhealth 添加一個回傳陳述句。然后在第二個代碼中將 mhealth 分配給common_monsterstat()函式。我建議你在 python 上學習函式、全域和區域變數 :)
這是修復代碼:
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return mmaxhealth
from monsterstats import *
mhealth= common_monsterstat()
def battlesys():
global mhealth
print("You challenged Monster!")
print("Monster's health: " mhealth)
uj5u.com熱心網友回復:
如果這只是設定資料,請不要使用函式。Yu 可以定義按名稱匯入的常量。在“monsterstatps.py”中:
import random
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
然后在你的“main.py”中:
from monsterstats import *
def battlesys():
print("You challenged Monster!")
print("Monster's health: " mhealth)
從長遠來看,您可能會定義class Stats包含這些值的 a,并匯入該類的實體。
uj5u.com熱心網友回復:
與其擁有所有自由浮動變數,不如將它們分組到一個類中可能更容易。例如,您可以擁有一個用于通用統計的類,并創建一個工廠函式來創建該類的實體。
class Stats:
def __init__(self, attack, defense, health, maxhealth):
self.attack = attack
self.defense = defense
self.health = health
self.maxhealth = maxhealth
def common_monsterstat():
mattack = random.randint(1, 5)
mdefense = random.randint(0, 1)
mhealth = random.randint(5, 10)
mmaxhealth = mhealth
return Stats(
attack=mattack,
defense=mdefense,
health=mhealth,
maxhealth=mmaxhealth,
)
在 main 方法中,您可以使用工廠方法來檢索統計資訊:
from monsterstats import *
stats = common_monsterstat()
def battlesys():
print("You challenged Monster!")
print("Monster's health: " stats.health)
為了使撰寫此類更加容易,您可以利用資料類,它非常適合純資料物件。
from dataclasses import dataclass
@dataclass
class Stats:
attack: int
defense: int
health: int
maxhealth: int
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483332.html
