我正在尝试实例化一个类:
class Customer(object):
def __init__(self, name, money, ownership):
self.name = name
self.money = money
self.ownership = ownership
def can_afford(self):
x = False
for bike in bikes.values():
if self.money >= bike.money * margin:
print "Customer {} can afford {}".format(self.name, bike)
x = True
if not x:
print "Customer {} cannot afford any bikes at this time.".format(self.name)
shoppers = {
# Initial condition of shopper1
"Jane": Customer("Jane", 200, False),
# Initial condition of shopper2
"Alfred": Customer("Alfred", 500, False),
# Initial condition of shopper3
"Taylor": Customer("Taylor", 1000, False)
}
buyer = Customer(name, money, ownership)
但buyer = Customer(name, money, ownership)
一直受到误导:
Undefined variable 'name'
Undefined variable 'money'
Undefined variable 'ownership'
但我想我用“Customer(...)”
在我的字典中设置了变量的值答案 0 :(得分:0)
class Customer(object):
def __init__(self, name, money, ownership):
self.name = name
self.money = money
self.ownership = ownership
def can_afford(self):
x = False
for bike in bikes.values():
if self.money >= bike.money * margin:
print "Customer {} can afford {}".format(self.name, bike)
x = True
if not x:
print "Customer {} cannot afford any bikes at this time.".format(self.name)
shoppers = {
# Initial condition of shopper1
"Jane": Customer("Jane", 200, False),
# Initial condition of shopper2
"Alfred": Customer("Alfred", 500, False),
# Initial condition of shopper3
"Taylor": Customer("Taylor", 1000, False)
}
#this is solution for your MENTIONED problem only
name = 'Buddy'
money = 2
ownership = 'something'
buyer = Customer(name, money, ownership)
上面提到的问题的解决方案,你只需要定义一些值。 但是还有其他一些问题,比如bikes.values(),从哪里来的?
记住,要使用某些东西,你必须首先告诉它是什么,所以定义一些东西。
希望它有所帮助。