编写类时出现Python错误?

时间:2016-12-20 00:54:28

标签: python class syntax-error

我正在解决以下问题:

编写购物车类以实现您经常在可以购买某些商品的网站上找到的购物车。想想你可以在购物车中存储什么东西,以及你可以在购物车上执行哪些操作。为简化问题,您可以将该网站视为电子电子商店,其中包括平板电视,收音机,iPod,便携式摄像机等产品。

到目前为止,这是我的代码:

class ShoppingCart(object):
    def __init__(self, name = "", address = ""):
        self.items = []
        self.total = 0
        self.shopper = name
        self.address = address

    def get_address(self):
        return self.address
    def get_address(self,address):
        self.address = address

    def add_item(self, T):
        "Add tuple(name, quantity, price, ID)"
    self.items.append(T)
    self.total = sum(t[2] for t in self.items)

    def delete_item(self, T):
        "Delete tuple(name, quantity, price, ID)"
        if T in self.items:
            self.items.remove(T)
            self.total = sum([t[2]] for t in self.items)

    def print_cart(self):
        print("\n cart:")
        print("\t", "Item", \t\, "price", "quantity")

    for T in self.items:
        print("\t", T[0],"\t", T[2], "\t", T[1])
        print("\n Total:", self.total)


    def test_cart():
        "Demonstrate use of class"
        s = ShoppingCart('Rich')
        s.add_item(("iPod Nano", 1, 150.00, '12345'))
        s.add_item(("The Holiday (DVD)", 2, 18.00, '14443'))
        s.set_address('123 Timber, St. Louis, MO, 63130')
        s.print_cart()

test_cart()

我收到错误说:

File "<ipython-input-5-b4071917f558>", line 27
    print("\t", "Item", \t\, "price", "quantity")
                                                 ^
SyntaxError: unexpected character after line continuation character

有谁知道为什么会出现这种错误?提前谢谢!

2 个答案:

答案 0 :(得分:1)

尝试添加冒号:

print("\t", "Item", "\t", "price", "quantity")

你可以用一种简单的方法:

print("\t Item \t price quantity")

答案 1 :(得分:0)

答案在于给出的错误信息。您无法打印\t\。我认为你的意思是"\t"