请修复这个商店的代码。我不知道如何修复它。

huangapple go评论73阅读模式
英文:

Please fix this shop code. I don't know how I can fix it

问题

  1. Total Value of stock:
    1 - 我在for循环中使用了变量self.k来显示库存商品,同时累加了值。然而,尽管商品被正确显示,但总值是不正确的:
    代码:
self.j = 1
self.k = 0
self.total = 0
print("==============================================")
print("\t\t\t\t\tSTOCK")
print("==============================================")
print("S.no\t\tItem Name\t\tItem Price\tItem Stock\t\tTotal Cost")
for i in range(1,self.n+1):
    print(self.j,"\t\t",self.item_names[self.k],"\t\t",self.item_prices[self.k],"\t\t",self.item_quantities[self.k],"\t\t\t",self.tots[self.k])
    self.total=self.total+self.item_prices[self.k]
    self.j+=1
    self.k+=1
    
print("==============================================")
print("\t\t\t\t\t\t\t\tTOTAL: ",self.total)
print("==============================================")
  1. Customer item selection system:
    这也没有工作,我不确定原因。我尝试使用嵌套的for循环来遍历商品列表并找到与用户选择匹配的商品。此外,即使它选择了商品,但在尝试显示用户想要购买多少时,也会显示错误。这是代码:
    代码:
for i in range(1,self.number):
    self.k = 0
    self.itmname = input("What product would you like to buy: ")
    for j in range(i):
        if self.itmname == self.item_names[self.k]:
            self.itmquantity = int(input("How much of this item would you like to buy: "))
            if self.itmquantity > self.item_prices[self.k]:
                print("This is not possible. We are setting it so that you have all the stock of that item.")
                self.itmquantity = self.item_prices[self.k]
                print("Your total is: ", self.tots[self.k])
                self.k=0
            else:
                print("Done.")
                print("Your total is: ",self.itmquantity*self.item_prices[self.k])
                self.k = 0
            
        else:
            self.k +=1

请注意,我已经去掉了HTML实体编码(例如")并纠正了缩进错误。这些更正应该有助于解决问题。

英文:

The following is a code for a shop. It has two problems:
1 - The total value when I print out the shop stock is not correct.
2 - In the customer function, the system for selecting items and their quantity isnt working too.

I'm new to python and I am not sure what to do here. I have been stuck for 2 days on this now. Please help me in fixing these two problems

#===========================================#SHOP#========================================================#
import random as rd

class shop():
    def __init__(self,name,address):
        self.name = name
        self.address = address

    def stock_input(self):
        self.n = int(input("Enter how many item you would like to input into the stock: "))
        self.item_list = []
        self.item_names = []
        self.item_prices = []
        self.item_quantities = []
        self.tots = []
        for i in range(1,self.n+1):
            print("")
            print("=========================================================================================")
            self.item_name = input("Enter item name: ")
            self.item_price = float(input("Enter item price: "))
            self.item_quantity = int(input("Enter stock of item: "))
            self.tot = self.item_price * self.item_quantity
            self.item = "NAME: ",self.item_name," PRICE: ",self.item_price," STOCK: ",self.item_quantity," TOTAL PRICE: ",self.tot
            self.item_names.append(self.item_name)
            self.item_prices.append(self.item_price)
            self.item_quantities.append(self.item_quantity)
            self.tots.append(self.tot)
            self.item_list.append(self.item_name)
            print("=========================================================================================")
        print("")
    
    def stock_display(self):
        self.j = 1
        self.k = 0
        self.total = 0
        print("")
        print("=========================================================================================")
        print("\t\t\t\t\tSTOCK")
        print("=========================================================================================")
        print("S.no\t\tItem Name\t\tItem Price\tItem Stock\t\tTotal Cost")
        for i in range(1,self.n+1):
            print(self.j,"\t\t",self.item_names[self.k],"\t\t",self.item_prices[self.k],"\t\t",self.item_quantities[self.k],"\t\t\t",self.tots[self.k])
            self.total=self.total+self.item_prices[self.k]
            self.j+=1
            self.k+=1
        
        print("=========================================================================================")
        print("\t\t\t\t\t\t\t\tTOTAL: ",self.total)
        print("=========================================================================================")
    
    def customer(self):
        self.nam = input("Enter customer name: ")
        self.random = "1234567890"
        self.bill_id = ""
        self.cart_names = []
        self.cart_price = []
        self.cart_quantity = []

        for i in range(1,5):
            i = rd.choice(self.random)
            self.bill_id+=i
        
        self.number = int(input("How many items would you like to buy: "))
        print("We have the following items: ", self.item_names)
        self.k = 0
        print("=========================================================================================")

        for i in range(1,self.n+1):
            print("Stock of ",self.item_names[self.k],": ",self.item_quantities[self.k],". PRICE: ",self.item_prices[self.k])
            self.k+=1
        
        print("=========================================================================================")
        self.k = 0
        for i in range(1,self.number):
            self.k = 0
            self.itmname = input("What product would you like to buy: ")
            for j in range(i):
                if self.itmname == self.item_names[self.k]:
                    self.itmquantity = int(input("How much of this item would you like to buy: "))
                    if self.itmquantity > self.item_prices[self.k]:
                        print("This is not possible. We are setting it so that you have all the stock of that item.")
                        self.itmquantity = self.item_prices[self.k]
                        print("Your total is: ", self.tots[self.k])
                        self.k=0
                    else:
                        print("Done.")
                        print("Your total is: ",self.itmquantity*self.item_prices[self.k])
                        self.k = 0
                
                else:
                    self.k +=1
            

object =shop(input("Enter shop name: "),input("Enter shop address: "))
object.stock_input()
object.stock_display()
object.customer()
  1. Total Value of stock:
    I used a variable self.k in a for loop to display the stock items while simultaneously adding up the values. However, while the items were being displayed properly, the total was not correct:
    Code:
self.j = 1
        self.k = 0
        self.total = 0
        print("")
        print("=========================================================================================")
        print("\t\t\t\t\tSTOCK")
        print("=========================================================================================")
        print("S.no\t\tItem Name\t\tItem Price\tItem Stock\t\tTotal Cost")
        for i in range(1,self.n+1):
            print(self.j,"\t\t",self.item_names[self.k],"\t\t",self.item_prices[self.k],"\t\t",self.item_quantities[self.k],"\t\t\t",self.tots[self.k])
            self.total=self.total+self.item_prices[self.k]
            self.j+=1
            self.k+=1
        
        print("=========================================================================================")
        print("\t\t\t\t\t\t\t\tTOTAL: ",self.total)
        print("=========================================================================================")
  1. Customer item selection system:
    This too isnt working and I am not sure why. I have tried using nested for loops to go through the items list and find the one that matches the user's choice. Also, even when it selects the item, it shows an error right after when it tries to show how much the user wants to buy. Here is the code:
    Code:
for i in range(1,self.number):
            self.k = 0
            self.itmname = input("What product would you like to buy: ")
            for j in range(i):
                if self.itmname == self.item_names[self.k]:
                    self.itmquantity = int(input("How much of this item would you like to buy: "))
                    if self.itmquantity > self.item_prices[self.k]:
                        print("This is not possible. We are setting it so that you have all the stock of that item.")
                        self.itmquantity = self.item_prices[self.k]
                        print("Your total is: ", self.tots[self.k])
                        self.k=0
                    else:
                        print("Done.")
                        print("Your total is: ",self.itmquantity*self.item_prices[self.k])
                        self.k = 0
                
                else:
                    self.k +=1

答案1

得分: 0

  1. 您必须将每种产品的价格乘以其数量,以获得正确的成本:
self.total = self.total + self.item_prices[self.k] * self.item_quantities[self.k]
  1. 客户商品选择系统更改后的代码下方。在注释中有解释。
self.number = int(input("您想购买多少件商品:"))

self.show_products()

self.k = 0

for i in range(0, self.number):
    self.k = 0
    self.itmname = input("您想购买哪种产品:")

    # 我们遍历item_list以检查itmname是否对应于产品
    for j in range(len(self.item_list)):
        if self.itmname == self.item_names[self.k]:
            self.itmquantity = int(input("您想购买这种商品的多少:"))

            # 我们检查所需的itmquantity是否超过产品的数量,而不是您之前做的价格
            if self.itmquantity > self.item_quantities[self.k]:
                print("这是不可能的。我们设置为您拥有该商品的全部库存。")

                # 我们将itmquantity设置为库存中的数量(而不是您之前的价格)
                self.itmquantity = self.item_quantities[self.k]
                # 将商品的数量设置为0
                self.item_quantities[self.k] = 0
                print("您的总价是:", self.itmquantity * self.item_prices[self.k])

                self.k = 0
            else:
                # 减去已售出的数量
                self.item_quantities[self.k] -= self.itmquantity
                print("完成。")
                print("您的总价是:", self.itmquantity * self.item_prices[self.k])
                self.k = 0

            # 显示更新后的商品及其数量
            self.show_products()

            break  # 当我们完成当前商品时,继续下一个商品
        else:
            self.k += 1


# 创建一个函数以提高可读性
def show_products(self):
    print("我们有以下商品:", self.item_names)
    self.k = 0
    print("=========================================================================================")

    for i in range(1, self.n + 1):
        print("商品", self.item_names[self.k], "的库存:", self.item_quantities[self.k], "。价格:",
              self.item_prices[self.k])
        self.k += 1

    print("=========================================================================================")
英文:
  1. You must multiply the price of each product with its quantity in order to have the correct cost:
self.total = self.total + self.item_prices[self.k] * self.item_quantities[self.k]
  1. Below the changed code of Customer item selection system. Explanation in the comments.
self.number = int(input("How many items would you like to buy: "))

self.show_products()

self.k = 0

for i in range(0, self.number):
    self.k = 0
    self.itmname = input("What product would you like to buy: ")

    # we loop over item_list so as to check if itmname corresponds to a product
    for j in range(len(self.item_list)):
        if self.itmname == self.item_names[self.k]:
            self.itmquantity = int(input("How much of this item would you like to buy: "))

            # we check if the wanted itmquantity is more than quantity of the product AND NOT its price as you previously did
            if self.itmquantity > self.item_quantities[self.k]:
                print("This is not possible. We are setting it so that you have all the stock of that item.")

                # we set itmquantity as the one in the stock (NOT as the price you had)
                self.itmquantity = self.item_quantities[self.k]
                # set quantity of item as 0
                self.item_quantities[self.k] = 0
                print("Your total is: ", self.itmquantity * self.item_prices[self.k])

                self.k = 0
            else:
                # Subtract the quantity sold
                self.item_quantities[self.k] -= self.itmquantity
                print("Done.")
                print("Your total is: ", self.itmquantity * self.item_prices[self.k])
                self.k = 0

            # Show up the products with the updated quantities
            self.show_products()

            break # continue in the next product when we are done with this
        else:
            self.k += 1


# Create a function so as to improve read-ability
def show_products(self):
    print("We have the following items: ", self.item_names)
    self.k = 0
    print("=========================================================================================")

    for i in range(1, self.n + 1):
        print("Stock of ", self.item_names[self.k], ": ", self.item_quantities[self.k], ". PRICE: ",
              self.item_prices[self.k])
        self.k += 1

    print("=========================================================================================")

huangapple
  • 本文由 发表于 2023年6月8日 17:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定