J. Doe
J. Doe

Reputation: 244

Finished with exit code 0?

I'm coding a shopping cart class to implement a shopping cart that i often find on websites where i can purchase some goods.Im thinking about stuff that i can store in a cart and also operations that i can perform on the cart. To simplify matters, i consider the website to be an electronics e-store that has goods like flat-panel TVs, boomboxes, iPods, camcorders, and so on. Here is my final code

class ShoppingCart(object):
    def __init__(self, s_name = ""):
        self.s_items = []
        self.s_total = 0
        self.s_shopper = s_name
        self.s_address = ""
    def add_item(self, s_T):
        self.s_items.append(s_T)
        self.s_total = sum([s_t[2]for s_t in self.s_items])
    def print_cart(self):
        print("\n Shipping:",self.s_address)
        print("\n Cart:")
        print("Name, \t\t ID, quantity, price")
        for s_t in self.s_items:
            print(s_t[0],"\t",s_t[3],"\t",s_t[1],"\t",s_t[2])
            print("\n Total:", self.s_total)
    def set_address(self,a):
        self.s_address = a
    def get_address(self):
        return self.s_address
    def demo(self):
        R = ShoppingCart('Karlson')
        R.add_item(('boom', 1, 23, 123))
        R.add_item(('baam', 2, 130, 242))
        R.set_address('123 main, smweher, MN')
        R.print_cart()

When i run the code, nothing happens and i got "Processed finished with exit code 0" Usually, when my code isnt working, i got syntax or indentation errors and being a noob in coding that got downvotes in here for 0 reason, i dont know if that error only happens in my machine or is it related to the code?

Upvotes: 0

Views: 877

Answers (1)

Daniel Pryden
Daniel Pryden

Reputation: 60917

You need to write some code at module scope to actually use your class. Looking at your code, you probably want something like this:

if __name__ == '__main__':
    cart = ShoppingCart()
    cart.demo()

Upvotes: 3

Related Questions