在Python中访问类中的项目?

时间:2019-02-24 21:16:33

标签: python-3.x class object

我对面向对象编程非常陌生,并且在运行main方法时无法访问类中的项目。我的程序正在尝试允许用户将商品价格添加到购物车中,直到价格完成为止,并打印商品数量和总计。

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

这是我选择“是”时遇到的错误:

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

这是我选择否时得到的输出:

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

2 个答案:

答案 0 :(得分:0)

您有很多错误,需要自己确定总价格和商品数量,需要确定具有现金收银机类的变量

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

答案 1 :(得分:0)

首先,您不使用类名在其方法中声明变量:为此您有self(您可以将其重命名为自己喜欢的名称,但'self'是约定)

第二步,您必须在主函数中初始化您的类对象,否则Python将不知道如何处理您调用的方法(当您在类中定义方法时,第一个参数self对于类对象,因此每次您初始化对象然后在该对象上调用方法时,您在方括号内传递的参数实际上是第二个参数,第一个是对象本身)

第三:这更多是一种样式,但是除了类名之外,您实际上并没有在python中使用CamelCase,其余的都在snake_case中

第四:+=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()

这可能就是我要这样做的方式,因为我不知道您是否真的需要它们,所以我取出了@property装饰器,您可以使用方括号{{ 1}}最后得到您想要的东西

然后,如果您真的想使用它,还应该做更多的事情,这将是异常捕获,确定如果将负值作为()传递,收款机的行为,等等。祝您好运,并喜欢Python