0

I am working on the following problem:

Write a shopping cart class to implement a shopping cart that you often find on websites where you could purchase some goods. Think about what things you could store in a cart and also what operations you could perform on the cart. To simplify matters, you could consider the website to be an electronics e-store that has goods like flat-panel TVs, radios, iPods, camcorders, and so on.

This is my code so far:

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()

I get an error saying:

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

Does anyone know why this error is occurring? Thank you in advance!

3
  • 1
    You missed the double quotes for the \t\ Commented Dec 20, 2016 at 0:57
  • Get a new error: NameError Traceback (most recent call last) <ipython-input-14-254519afb7dc> in <module>() 1 #10 ----> 2 class ShoppingCart(object): 3 def __init__(self, name = "", address = ""): 4 self.items = [] 5 self.total = 0 <ipython-input-14-254519afb7dc> in ShoppingCart() 14 def add_item(self, t): 15 "Add tuple(name, quantity, price, ID)" ---> 16 self.items.append(t) 17 self.total = sum(t[2] for t in self.items) 18 NameError: name 'self' is not defined Commented Dec 20, 2016 at 2:24
  • You have wrong indentation for the code in add_item(self, T) method. Commented Dec 20, 2016 at 2:30

2 Answers 2

1

Try to add colon:

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

And you could use a simple way:

print("\t Item \t price quantity")
Sign up to request clarification or add additional context in comments.

4 Comments

You added quotes, not colons.
Thank you so much! I seem to now be getting another error:
Now getting a new error that says "self" is not defined in lines 2 or 16...any idea why?
You have bad indentations in your add_item method.
0

The answer lies in the error message given. You cannot print out \t\. I think you meant "\t".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.