所以我为学校项目制作基于文本的角色扮演游戏,但我不断收到此错误
“player”没有属性“playerHealth”。您指的是“playerStats”吗?
我尝试设置全局变量,但这也不起作用
global playerResponse
playerResponse = input("Welcome brave adventurer, to get started enter 'start' but if you wish to familiarize yourself with your character, enter 'stats'")
#player stats
class player:
def playerStats(self, playerAC, playerMaxHealth, playerHealth, playerStrength, playerCrit, playerPhysical, playerMagic):
self.playerAC = 14
self.playerMaxHealth = 100
self.playerHealth = 100
self.playerStrength = 30
self.playerCrit = 15
self.playerPhysical = 20
self.playerMagic = 5
#display stats to player
def statsDisplay():
print("##YOUR STATS##")
print(player.playerAC)
print(player.playerHealth , "/" , player.playerMaxHealth)
print(player.playerStrength)
while player.playerHealth < 0:
if playerResponse == "stats":
statsDisplay()
您收到错误是因为类
player
确实没有名为 playerHealth
的属性。因此,修改类 player
以包含 playerHealth
的属性,并在 playerStats
中初始化它。这是为您修改的代码。
class Player:
def __init__(self):
self.playerAC = 14
self.playerMaxHealth = 100
self.playerHealth = 100
self.playerStrength = 30
self.playerCrit = 15
self.playerPhysical = 20
self.playerMagic = 5
#display stats to player
def statsDisplay(player):
print("##YOUR STATS##")
print(player.playerAC)
print(player.playerHealth , "/" , player.playerMaxHealth)
print(player.playerStrength)
#initialize player object
player = Player()
while player.playerHealth < 0:
if playerResponse == "stats":
statsDisplay(player)
请告诉我这是否对你有用。