未定义的功能令人困惑

时间:2015-11-04 22:16:34

标签: python

我在这里有一些代码,我想知道为什么我得到“total_with_tax未定义”

当我这样做时会发生这种情况:

c = Customer()
c.print_bill() --> this is where I get the error

代码:

class Customer:
def __init__(self):
    self.total = 0
    self.items_ordered = str("")

def add_to_order(self, NameOfItem, CostOfItem):
    self.total += CostOfItem
    self.items_ordered = self.items_ordered + (str(NameOfItem) + ", ")

def total_with_tax(self):
    return ((self.total * 0.13) + self.total)

def print_bill(self):
    print("----------------------------------------------")
    print(self.items_ordered)
    print("$%d" %(self.total))
    print("$%d" %(total_with_tax()))
    print("----------------------------------------------")

1 个答案:

答案 0 :(得分:2)

您需要在total_with_tax前加self,如下所示:

 print("$%d" % self.total_with_tax())