使用 pandas 的产品库存项目

Product Inventory project using pandas


我正在尝试制作一个简单的 python 程序,我们必须创建一个应用程序来管理 products.To 的库存,创建一个产品 class,它有价格、ID 和手头的数量。然后创建一个库存class,它跟踪各种产品并可以总结库存价值。

请检查下面的代码,

输入

import pandas as pd

class Product:
    def __init__(self):
        self.price = None
        self.id = None
        self.qty = None
        self.data = pd.DataFrame(([]),columns=['ID','Cost','Quantity'])
class inventory(Product):
        def value(self):
            while True:
                    print("Please give your product details,")
                    self.cost = float(input("Cost of the product : "))
                    self.id = int(input("ID of the product : "))
                    self.qty = int(input("Quantity of the product : "))
                    print("==============================================================================")
                    self.data = self.data.append({'ID':self.id,'Cost':self.cost,'Quantity':self.qty},ignore_index=True)
                    print(self.data)
                    print("==============================================================================")
                    print("1)Would u like to add even more products?\n2)Get the inventory value\n3)Exit")
                    option = int(input())
                    if(option == 1):
                        inventory.value(self)
                    elif(option==2):
                        print("The total value of inventory is : ",((self.data['Cost'])*(self.data['Quantity'])).sum())
                    else:
                        print("Exiting....")
                        exit()
                        break
            return


inv = inventory()
inv.value()



输出

Please give your product details,                                                                                                                              
Cost of the product : 10                                                                                                                                       
ID of the product : 11                                                                                                                                         
Quantity of the product : 12                                                                                                                                   
==============================================================================                                                                                 
     ID  Cost  Quantity                                                                                                                                        
0  11.0  10.0      12.0                                                                                                                                        
==============================================================================                                                                                 
1)Would u like to add even more products?                                                                                                                      
2)Get the inventory value                                                                                                                                      
3)Exit                                                                                                                                                         
2                                                                                                                                                              
The total value of inventory is :  120.0                                                                                                                       
Please give your product details,                                                                                                                              
Cost of the product : 12                                                                                                                                       
ID of the product : 12                                                                                                                                         
Quantity of the product : 12                                                                                                                                   
==============================================================================                                                                                 
     ID  Cost  Quantity                                                                                                                                        
0  11.0  10.0      12.0                                                                                                                                        
1  12.0  12.0      12.0                                                                                                                                        
==============================================================================                                                                                 
1)Would u like to add even more products?                                                                                                                      
2)Get the inventory value                                                                                                                                      
3)Exit                                                                                                                                                         
3                                                                                                                                                              
Exiting....  


在我按 2 之后,我期待我的程序给我价值并告诉我,1)你想添加更多产品吗?2)获取库存价值3)退出

我该怎么做?另外,如果您发现任何修改或任何建议,请在下方告诉我。

试试这个

import pandas as pd

class Product:
    def __init__(self):
        self.price = None
        self.id = None
        self.qty = None
        self.data = pd.DataFrame(([]),columns=['ID','Cost','Quantity'])
class inventory(Product):
    def value(self):
        while True:
            option = 1
            if len(self.data):
                print("1)Would u like to add even more products?\n2)Get the inventory value\n3)Exit")
                option = int(input())
            if option == 1:
                print("Please give your product details,")
                self.cost = float(input("Cost of the product : "))
                self.id = int(input("ID of the product : "))
                self.qty = int(input("Quantity of the product : "))
                print("==============================================================================")
                self.data = self.data.append({'ID':self.id,'Cost':self.cost,'Quantity':self.qty},ignore_index=True)
                print(self.data)
                print("==============================================================================")
                inventory.value(self)
            elif option == 2:
                print("The total value of inventory is : ",((self.data['Cost'])*(self.data['Quantity'])).sum())
            else:
                print("Exiting....")
                exit()

inv = inventory()
inv.value()

我个人会删除 while true 循环并创建函数来完成您的任务:

import pandas as pd

class Product(object):
    def __init__(self):
        self.price = None
        self.id = None
        self.qty = None
        self.data = pd.DataFrame(([]),columns=['ID','Cost','Quantity'])

class inventory(Product):
    # create a prod_detail function
    def prod_detail(self):
        print("Please give your product details,")
        self.cost = float(input("Cost of the product : "))
        self.id = int(input("ID of the product : "))
        self.qty = int(input("Quantity of the product : "))
        print("="*30)
        self.data = self.data.append({'ID':self.id,'Cost':self.cost,'Quantity':self.qty},ignore_index=True)
        print(self.data)
        print("="*30)
        self.option()
    # create an options function
    def option(self):
        print("1)Would u like to add even more products?\n2)Get the inventory value\n3)Exit")
        option = int(input())
        if(option == 1):
            self.prod_detail()
        elif(option==2):
            print("The total value of inventory is : ",((self.data['Cost'])*(self.data['Quantity'])).sum())
            self.option()
        else:
            print("Exiting....")
            exit()

    def value(self):
        # remove the while true loop and just run the prod_detail function
        self.prod_detail()


inv = inventory()
inv.value()

Please give your product details,
Cost of the product :  1
ID of the product :  2
Quantity of the product :  3
==============================
    ID  Cost  Quantity
0  2.0   1.0       3.0
==============================
1)Would u like to add even more products?
2)Get the inventory value
3)Exit
 2 <--------
The total value of inventory is :  3.0
1)Would u like to add even more products?
2)Get the inventory value
3)Exit