這是我收到的錯誤:爲什麼忽略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)以供參考。如果您認爲其他人不能看到該代碼,請將該帖子標記爲「其他」,並說明情況。問候。 –