2016-03-06 108 views
-2

這是我收到的錯誤:爲什麼忽略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.' 
+0

你認爲一個變量中有一些東西,但你的程序認爲不是。現在是打印該變量以查看它是否具有您的想法的好時機。 – tdelaney

+1

如果您認爲它解決了您的問題,請不要編輯您的帖子以表明您的問題已解決,而是請[接受](http://meta.stackexchange.com/questions/5234)答案。它將使整個社區認識到正確的解決方案。這可以通過點擊答案旁邊的綠色複選標記來完成。請參閱此[圖片](http://i.stack.imgur.com/uqJeW.png)以供參考。如果您認爲其他人不能看到該代碼,請將該帖子標記爲「其他」,並說明情況。問候。 –

回答

3

如果if語句前加上一個print result你會得到一個提示,那爲什麼發生這種情況,因爲它會輸出['p', 'l', 'e', 'a', 's', 'e', 'h', 'e', 'l', 'p'](假設你('please', 'help)的說法。請記住這個作爲打印語句是最快捷方式這種性質的調試問題(通常我去到更廣泛的測試之前)

這樣做的原因是使用了+ =運營商(result += arg) - 開關這一個append methodresult.append(arg)),它應該工作

您的第二個問題請查看Calling a Class Method From Another Class Method - 因爲這提供了一個乾淨的方式來做你正在試圖做的事情VendingMachine

1

正如JGreenwell指出的,你得到這個的原因是因爲你的結果是一個列表。因此它收集所有字符以形成一個列表:

['p', 'l', 'e', 'a', 's', 'e'] 

而不是字符串'請'。在這裏,我提出了一個替代的方式來解決這個問題:初始化的結果字符串:

result = '' 

這樣,您將得到的結果你想

+0

非常感謝!但爲什麼這會使它不同?不只是列表的字符串? –

+0

它確實與你的建議工作!我只是不明白爲什麼 –

+0

不,字符串不是列表...我認爲JGreenwell已經做了一個示範。列表可以包含列表,字符,數字等任何內容,但是你想要的是一個字符串。因此,您應該聲明爲字符串:result ='' – Ian