python - 用 Python 制作收银机

标签 python python-3.x class

我正在尝试在Python 3.6上制作一个商店程序,但我的代码无法正常工作。我做了很多修改,现在我遇到了更多内部编码问题,例如使菜单循环运行直到顾客退出该程序并在列表内创建包含各个项目的所有信息的列表。代码在这里可能无法正确缩进,但这更多的是复制和粘贴错误。

代码如下:

class RetailItem:
    def __init__(self):
        self.price = price
        self.units = units
        self.description = description


class CashRegister:
    def __init__(RetailItem, self):
        self.totalprice=0
        self.cart=[]
        self.items=[]
        duck = RetailItem
        duck.description = "Large plush duck"
        duck.units = 3
        duck.price = 6.99

        porcupine = RetailItem
        porcupine.description = "Pink plush porcupine"
        porcupine.units = 5
        porcupine.price = 9.99

        bunny = RetailItem
        bunny.description = "small white plush bunny"
        bunny.units = 7
        bunny.price = 4.99

        self.items=[duck,bunny,porcupine]
        print("Welcome to The Toy Store.\n Selection:\n 1.Duck \n 2.Bunny \n 3.Porcupine")
    def menu(self):
        item = int(input("What would you like to select? "))
        item=+1
        item = self.items[item]
        return item
    def purchase(RetailItem):
        self.items = self.items.append(item)
        self.totalprice = totalprice.append(item.price)
        item.units=item.units-1
        self.cart= self.items.append(item)
        print("You have added"+(item)+"to your cart")
    def get_total(self):
        return("Your total is", self.totalprice)
    def show_item(RetailItem):    
        return(description(RetailItem), units(RetailItem), price(RetailItem))
    def clear_register(self):
        self.totalprice = []
        self.items = []

def main():

C1=CashRegister(CashRegister)

C1.menu()

choice=int(input(("Options: \n 1.Add to Cart 2. View item \n 3.Return to Main Menu ")))
if choice==1:
    purchase(item)
if choice==2:
    show_item(item)
    choice=int(input(("Would you like to purchase this item? \n 1. Yes \n 2. No"))
if choice==3:
    C1.menu()
choice=int(input(("Options: \n 1. Add more items to your cart. 2.View cart 3. Quit"))
if choice!= 3:
    if choice==1:
           menu()

最佳答案

似乎 def menu(self) 不在任何类中,因此它不应该有 self 参数。删除它,它应该可以工作。或者将 menu 放入一个类中。

self 参数是为类方法保留的。更多信息here

编辑:实际上,如果我理解您的代码,您的 menu(self) 属于 CashRegister 类。如果是这样,只需缩进整个 menu(self) block 即可。

class CashRegister:
    def __int__(self,RetailItem):
        self.total= ()
        Duck = RetailItem()
        Duck.description = "Large plush duck"
        Duck.units = 3
        Duck.price = 6.99

        Porcupine = RetailItem()
        Porcupine.description = "Pink plush porcupine"
        Porcupine.units = 5
        Porcupine.price = 9.99

        Bunny = RetailItem()
        Bunny.description = "small white plush bunny"
        Bunny.unitsininv = 7
        Bunny.price = 4.99

    def menu(self):
        print("Welcome to The Toy Store.\n Selection:\n 1.Bunny \n 2.Porcupine \n 3.Duck")
        choice = int(input("What would you like to buy? "))
        if choice==1:
            item = Bunny
        elif choice == 2:
            item = Porcupine
        elif choice==3:
            item = Duck
        else:
            print("Error")

关于python - 用 Python 制作收银机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838904/

相关文章:

python - 在 Spyder(Python 3.6)中导入 cv2 时导入错误

python - 为什么 justify 和anchor 对于 Python Tkinter 的 Label 不起作用

javascript - 使用 Javascript 更改类

python - python类中私有(private)变量的实际实现

c++ - 这个类声明是什么意思?

python - 在 Heroku 上托管时,Selenium 应用程序重定向到 Cloudflare 页面

python - 如何指定 Tkinter 窗口打开的位置?

python - 是否有在 Python 代码中指示数量单位的约定?

python - 在 Fipy 中求解多个偏微分方程

python - 执行 os.popen(...) 时出现 ValueError