class Inventory(object):
def __init__(self):
self.inventory = {
'cash': 500,
'paycheck': 1,
'savings': 1000,
'current_car': 0,
'possible_cars': ['Chevy', 'Honda', 'BMW'],
'car_cost': [0, 100, 200],
'current_house': 0,
'possible_houses': ['apartment','townhouse','suite'],
'house_cost': [0, 150, 300],
'status': self.status()
}
def status(self):
while self.inventory['cash'] + self.inventory['savings'] > 0:
return True
我目前正在完成练习 45 中的“Learn Python the Hard Way”。我创建了一个类来列出与我的游戏相关的项目,我已经将这些键和值存储在我的init方法下的字典中。我遇到问题的地方是我的最后一个键和它的值——“状态”键。
我想要这个值做的是引用我的状态方法,只要我的玩家有一个正数的钱,我就将它设置为返回真(我的游戏的其他部分将引用 .inventory['status'] 来检查在它们执行之前它的真实性现在我已经快速完成了两行概念验证代码来验证是否可以将函数用作值 - 我要挂断的是如何在类中实现它,特别是当我的字典在init中时。
我的错误:
Traceback (most recent call last):
File "ex45file1.py", line 151, in <module>
my_inv = Inventory() #TEST
File "ex45file1.py", line 80, in __init__
'status': status()
NameError: global name 'status' is not defined
我在哪里错了?