运行代码时,我收到错误消息: “ AttributeError:类型对象'Player'没有属性'hkslist'”
为什么我不能从班级内调用列表?想法是,该列表应该从列表中选择两个功能之一,然后在被调用时运行该功能。
完整代码:
import random
from time import *
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Player:
coins = 100
health = 100
p = 3 #Potions
hoursslept = 0
def __init__(self):
random.seed()
print("init instance variables here")
#self.coins = random.randint(100, 500) Förlegad kod.
def hk1():
print("Du sprang iväg")
def hk2():
#print("Du försökte springa iväg men trillade och slog i knät: - 20 HP")
print("Du sprang iväg")
sleep(3)
print("men du trillar och slår i knät: -20 HP")
self.health = self.health - 20
print("Your health is: " + str(self.health), " HP")
#Fortsätt 'storyn'
def hkf1():
print("Du besegrade håkan")
print("Du tar hans droger och säljer det själv till Eleverna: +150 coins")
print("Your health is ", str(self.health), " HP")
self.coins = self.coins + 150
print("You have: ", str(self.coins), " coins")
def hkf2():
print("Håkan besegrade dig: -50 HP")
print("Your health is ", str(self.health), " HP")
print("You have: ", str(self.coins), " coins")
self.coins = self.coins + 150
self.hkslist = [hk1, hk2]
self.hkflist = [hkf1, hkf2]
self.indhks = random.randint(0,len(self.hkslist)-1)
self.indhkf = random.randint(0,len(self.hkflist)-1)
def report_status(self):
status_message = "Your health is %s, \nCoins left %s" % (self.health, self.coins)
return status_message
william = Player()
hakan = Player()
print("Welcome to a poorly made text-based game:")
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
print("You have: ", str(william.coins), " coins")
while True:
print("Commands: add, drink, coins, sleep, quest")
a = input("Enter a command: ")
if a == "add" or a == "Add" or a == "buy" or a == "Buy":
if william.coins == 0:
print("You can't afford a potion")
else:
william.coins = william.coins - 25
william.p = Player.p + 1
print("You bought a potion for 25 coins")
print("You have ", william.coins, " coins left")
print("you have ", william.p, " potions")
print("Your health is now ", str(william.health), " HP")
if a == "drink":
if Player.p == 0:
print("You can't drink any potions.")
print("You have zero potions left!")
print("Your health is ", str(william.health), " HP")
elif william.health >= 250:
print("Your health is already maxed out. You can't drink a potion.")
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
else:
william.health = william.health + 20
william.p = william.p - 1
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
if a == "sleep":
if william.health >= 250:
print("Your health is already maxed out. You can't drink a potion.")
print("Your health is ", str(william.health), " HP")
else:
william.health = william.health + 5
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
if a == "I" or a == "i" or a == "inv" or a == "inventory":
if william.p == 0:
print("Your backpack is empty")
else:
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
if a == "quest":
quest = input("Choose from quest: 1, 2 or 3 ")
if quest == "1" or quest == "option 1" or quest == "opt 1":
print("Du vandrar runt på Rudbeck när du ser Håkan, din samhällslärare, i ett mörkt hörn")
print("Du väljer att gå närmare för att investigera.")
print("Håkan står och säljer knark till eleverna.")
hk = input("Vad gör du? Spring: Slåss:")
if hk == "Spring" or hk == "spring":
Player.hkslist[Player.indhks]()
if hk == "slåss" or hk == "Slåss" or hk == "s":
Player.hkflist[Player.indhkf]()
if a == "coins":
if william.coins >= 500:
print("You're filthy rich!!!")
print("You have: ", str(william.coins), " Coins")
else:
print("You have: ", str(william.coins), " Coins")
#Debug tools
if a == "add coin" or a == "AC" or a == "ac" or a == "more":
extracoins = input("How many coins do you want?: ")
Player.coins = int(extracoins)
print("You now have: ", str(william.coins), " Coins")
答案 0 :(得分:3)
hkslist
是Player
类实例的属性,而不是Player
类本身的实例。您可以在hkslist
(self.hkslist = [hk1, hk2]
)的定义中看到这一点,其中hkslist
是在self
上定义的。如果要访问hkslist
,则需要创建Player
的实例。例如:
player = Player()
player.hkslist[player.indhks]()