为什么忽略if语句中的这个条件?

时间:2016-03-06 03:24:23

标签: python class

这是我收到的错误:

   >>> 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.'

2 个答案:

答案 0 :(得分: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 :(得分:1)

正如JGreenwell所指出的,你之所以得到这个,是因为你的结果是一个清单。因此,它收集所有字符以形成一个列表:

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

而不是字符串'please'。在这里,我提出了另一种解决方法:将结果初始化为字符串:

result = ''

这样您就可以得到想要的结果