为什么我收到此属性错误?
class GameState(object):
"""Keeps track game state variables"""
def __init__(self, convo_flag=0, characters_talked_to=0, convo_log=(None)):
self.convo_flag = convo_flag
self.characters_talked_to = characters_talked_to
self.convo_log = convo_log
def welcome_screen():
global LAST_NAME
global BULLY
global DAY
raw_input(messages.WELCOME)
LAST_NAME = raw_input(messages.LAST_NAME)
BULLY = characters.random_character(cclass='Camper', gender='m')
print 'Your name is Pickett %s' % LAST_NAME
messages.print_messages([
messages.EXPLANATION,
messages.BUS_LOADING,
messages.CRACK,
messages.GAME_KID_LOST])
return week_one(DAY)
def week_one(day):
if day == 1:
messages.print_messages(messages.WEEK_ONE[day])
campers = characters.random_character_sample(cclass='Camper', count=5)
people_outside_theater = campers + [characters.TROID]
while GameState.characters_talked_to != 3:
我不知道为什么我得到这个属性错误,我完全在那个构造函数中声明它,是否有一些我缺少的是我需要在构造函数之外声明它?这真的是我的大脑。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pickett.py", line 44, in welcome_screen
return week_one(DAY)
File "pickett.py", line 52, in week_one
while GameState.characters_talked_to != 3:
AttributeError: type object 'GameState' has no attribute 'characters_talked_to'
答案 0 :(得分:3)
您需要创建一个实例,以便您像这样使用您的类:
gameState = GameState()
while gameState.characters_talked_to != 3:
在您的代码中,您尝试访问未在班级中定义的class-level attribute。
答案 1 :(得分:3)
__init__
个characters_talked_to
函数设置了self
GameState
GameState
的实例{/ 1}}。
您没有在GameState
上设置它,这是一个类。
您也没有创建__init__
的任何实例,因此在任何情况下{{1}}都不会被您的程序调用。