-2

I am very new to Object oriented programming and I am having trouble accessing items in my class when I run my main method. My program is trying to allow a user to add item prices to a cart until they are finished and prints the number of items and total.

class CashRegister:
    print("Welcome to shopping world!")
    def __init__(self, price):
        self.price = price
    def addItem(self, price):
        CashRegister.totalPrice = CashRegister.totalPrice + price 
        CashRegister.itemCount = CashRegister.itemCount + 1
    @property
    def getTotal(self):
        return totalPrice
    @property
    def getCount(self):
        return itemCount
def main():
    selection = "Y"
    while selection != "N":
        selection = input("Would you like to add another item to the 
cart Y or N")
        selection = selection.upper()
        if  selection == "Y":
            price = input("What is the price of the item?")
            CashRegister.addItem(price)
        else:
            print(CashRegister.getCount)
            print(CashRegister.getTotal)
            print(selection)
main()

Here is the error I am getting when I select yes:

TypeError: addItem() missing 1 required positional argument: 'price'

Here is the output I am getting when I select no:

Welcome to shopping world!
Would you like to add another item to the cart Y or Nn
<property object at 0x0000022CFFCA2598>
<property object at 0x0000022CFFCA2548>
N
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Daniel Robert
  • 15
  • 1
  • 6
  • 1
    addItem is an *instance* method, it's unclear why you're calling it on a *class*. – jonrsharpe Feb 24 '19 at 21:21
  • You should just call `getTotal` `total`. Or else you almost might as well just use a normal getter method. That being said, you probably should just not use properties until you have the basics of classes down. I suggest the taking a look at the [official python tutorial on classes](https://docs.python.org/3/tutorial/classes.html). It is pretty approachable. It really is good to just go through the motions and type in the examples into an interpreter for yourself. – juanpa.arrivillaga Feb 24 '19 at 21:24
  • If you want to use `addItem` this way, you need to use the `@staticmethod` decorator for `addItem`. Check this link https://stackoverflow.com/questions/735975/static-methods-in-python – maf88 Feb 24 '19 at 21:31

2 Answers2

0

you have many mistakes you need the determine totalprice and itemcount in self, you need to determine a variable with cashregister class

class CashRegister:
    print("Welcome to shopping world!")
    def __init__(self):
        self.totalPrice=0
        self.itemCount=0
    def addItem(self, price):
        self.totalPrice = self.totalPrice + price 
        self.itemCount = self.itemCount + 1
    @property
    def getTotal(self):
        return self.totalPrice
    @property
    def getCount(self):
        return self.itemCount
def main():
    selection = "Y"
    box=CashRegister()
    while selection != "N":
        selection = input("Would you like to add another item to thecart Y or N\n\t:")
        selection = selection.upper()
        if  selection == "Y":
            price = input("What is the price of the item?\n\t:")
            box.addItem(int(price))
        else:
            print(box.getCount)
            print(box.getTotal)
            print(selection)
main()
0

first, you don't use the class name for declaring variables in its methods: you have self for that (which you can rename to whatever you like, but 'self' is convention)

second, you have to initialize your class object in the main function, otherwise Python won't know what to do with the methods you call (when you define a method in a class, the first argument self stands for the class object, so each time you initialize an object and then call a method on that, the argument you pass inside the brackets is actually the second argument, first one being the object itself)

third: this is more of a style thing, but you don't really use CamelCase in python except for names of classes, all the rest is in snake_case

fourth: += is more readable and faster than example = example + 1

class CashRegister(object) :

    def __init__(self) :
        self.total_price = 0
        self.item_count = 0

    def add_item(self, price) :
        self.total_price += int(price)
        self.item_count += 1

    def get_total(self) :
        return self.total_price

    def get_count(self) :
        return self.item_count

def main() :
    register = CashRegister()
    selection = True

    while selection :
        selection = input("Would you like to add another item to the cart Y or N\n\t").upper()

        if selection == "Y" :
            price = input("What is the price of the item?\n\t")
            register.add_item(price)

        else :
            print(register.get_total())
            print(register.get_count())
            print(selection)
            selection = False

main()

this is how I would probably do it, I've taken out the @property decorators because I don't know if you really have a need for them there, you can just call the methods with brackets () at the end to get what you want

Then, there's a bunch more stuff you should do if you really want to to use this, and that would be exception catching, determine how the cash register behaves if a negative value is passed as price, and so on... good luck and enjoy Python

lupodellasleppa
  • 124
  • 1
  • 1
  • 11