2016-03-06 74 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