这是我收到的错误:为什么忽略if语句中的这个条件?
>>> m.ask('please vend')
Traceback (most recent call last):
File "/Users/Documents", line 196, in ask
getattr(self, 'vend')
AttributeError: 'MissManners' object has no attribute 'vend'
的VendingMachine
级工作正常,但MissManners
类没有。我不知道为什么。
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Machine is out of stock.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'You must deposit $10 more.'
>>> v.deposit(7)
'Current balance: $7'
>>> v.vend()
'You must deposit $3 more.'
>>> v.deposit(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.deposit(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.deposit(15)
'Machine is out of stock. Here is your $15.'
>>> w = VendingMachine('soda', 2)
>>> w.restock(3)
'Current soda stock: 3'
>>> w.deposit(2)
'Current balance: $2'
>>> w.vend()
'Here is your soda.'
"""
"*** YOUR CODE HERE ***"
def __init__(self, product, price):
self.itemName = product
self.cost = price
self.stock = 0
self.balance = 0
def vend(self):
if self.stock == 0:
print(" 'Machine is out of stock.' ")
elif self.balance < self.cost:
print(" 'You must deposit $" + str(self.cost - self.balance) + " more.'")
elif self.cost < self.balance:
print(" 'Here is your " + self.itemName + " and $" + str(self.balance - self.cost) + " change.'")
self.balance = 0
self.stock -= 1
else:
self.balance -= self.cost
self.stock -= 1
print("'Here is your " + self.itemName + ".'")
def restock(self, amount):
self.stock += amount
print("'Current " + self.itemName + " stock: " + str(self.stock) + "'")
def deposit(self, amount):
if self.stock == 0:
print("'Machine is out of stock. Here is your $" + str(amount) + ".'")
else:
self.balance += amount
print("'Current balance: $" + str(self.balance) + "'")
class MissManners:
"""A container class that only forward messages that say please.
>>> v = VendingMachine('teaspoon', 10)
>>> v.restock(2)
'Current teaspoon stock: 2'
>>> m = MissManners(v)
>>> m.ask('vend')
'You must learn to say please first.'
>>> m.ask('please vend')
'You must deposit $10 more.'
>>> m.ask('please deposit', 20)
'Current balance: $20'
>>> m.ask('now will you vend?')
'You must learn to say please first.'
>>> m.ask('please hand over a teaspoon')
'Thanks for asking, but I know not how to hand over a teaspoon.'
>>> m.ask('please vend')
'Here is your teaspoon and $10 change.'"""
def __init__(self, *args):
self.ask
def ask(self, *args):
result = ''
for arg in args:
if type(arg) == str:
result += arg
elif type(arg) == int:
balance = arg
if 'please' in result:
if 'deposit' in result:
self.deposit(balance)
elif 'vend' in result:
self.vend()
else:
return 'You must learn to say please first.'
你认为一个变量中有一些东西,但你的程序认为不是。现在是打印该变量以查看它是否具有您的想法的好时机。 – tdelaney
如果您认为它解决了您的问题,请不要编辑您的帖子以表明您的问题已解决,而是请[接受](http://meta.stackexchange.com/questions/5234)答案。它将使整个社区认识到正确的解决方案。这可以通过点击答案旁边的绿色复选标记来完成。请参阅此[图片](http://i.stack.imgur.com/uqJeW.png)以供参考。如果您认为其他人不能看到该代码,请将该帖子标记为“其他”,并说明情况。问候。 –