python - 无法从课堂内呼叫列表。
问题描述
当我运行代码时,我收到错误消息:“AttributeError: type object 'Player' has no attribute '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")
解决方案
hkslist
是非类本身的实例的属性Player
。您可以在( )的定义Player
中看到这一点。如果你想访问,你需要创建一个. 例如:hkslist
self.hkslist = [hk1, hk2]
hkslist
self
hkslist
Player
player = Player()
player.hkslist[player.indhks]()
推荐阅读
- python - 如何使用 scrapy 抓取 google play store
- unity3d - 为什么我的 Unity FPS 在 Build 中下降,但在 Editor 中却没有?
- firebase - 从 npm 安装的 firebase 部署和二进制文件?
- assembly - 如何将浮点常数移动到 FP 寄存器中?
- java - springboot 1.5.3 如何获取全局健康状态执行器
- java - Scope_prototyped bean 在启动时被实例化并失败,因为它不能自动装配
- pdf - 使用 PDFBox 或其他东西,是否可以知道 pdf 是否不包含扫描页面?
- javascript - Javascript如何将值重置为原始值
- java - 添加改变绘图颜色的按钮
- r - Rbind 列表中具有名称的数据帧