连续循环,用户输入购物清单 - 添加总计和打印总计

Continuous Loop with User Input for Shopping List - Adding the Total and Printing Grand Total

提问人:Rendevouz 提问时间:10/13/2023 最后编辑:BarmarRendevouz 更新时间:10/13/2023 访问量:62

问:

  1. 我创建了一个空字典,所以当用户使用该程序时, 他们购买的物品将被添加。
  2. 最后,在他们退出程序时打印。
  3. 我希望用户能够在连续循环中购买,从 产品(此处未看到)的产品,直到他们决定退出该计划。 每次计算每重量的总成本 * - 问题区域!!
  4. 当用户决定退出 (2) 时,提供/计算总重量 * 价格(每千克) - 不要 知道吗!!
  5. 我也不能将杂货清单打印到屏幕上 - 在这里发行!!
# dictionary to store productID("ID"), price("price")
#with empty list as their values
groceryList ={"productID":[], "weight": [], "price":[]}
 
# converting dictionary to list for further updation
buyersList = list(groceryList.values())
 
# variable iD value of "productID" from dictionary 'buyersList'
iD = buyersList[0]

# variable weight value of "quantity" from dictionary 'buyersList'
kG = buyersList[1]

# variable qu value of "price" from dictionary 'a'
productPrice = buyersList[2]
 
total = 0
 
# This loop terminates when user select 2.EXIT option when asked
# in try it will ask user for an option as an integer (1 or 2) 
# if correct then proceed with an if/else statement asking for user input

# It should continue and keep adding the total price * weight - BUT THIS FUNCTION DOESN'T WORK

while True:
    try:
        choice = int(input("1.SELECT\n2.EXIT\nPlease select 1 or 2 (from the menu) here : ")) #use new line formatting
    if choice == 1:
        i = int(input("Please enter the product ID here : "))
        # input quantity of product
        w = float(input("Please enter how many KG you would like : "))
    
        p = float(input("Please enter the price of the product here : "))

        # append value of in "name", "quantity", "price"
        iD.append(i)  
        # as na = b[0] it will append all the value in the
        # list eg: "name":["rice"]
        productPrice.append(p)     
 
        # after appending new value the sum in price
        # as to be calculated
        total = lambda w, p: w * p
        print("\nYour current total amount is", total(w, p))
            
    elif choice == 2:
        break
        
    else:
        print("Sorry, that option isn't available. Please, try again.")
        
except ValueError:
    print("Invalid response. Please try again.")
    
#when user asks to exit loop - their grocery list is printed to screen
print("\n\n\nGROCERY LIST")

for i in range(len(buyersList)):
  print(iD[i], kG[i], productPrice[i])

我想打印他们所有选项的总和

python 字典 输入 while-loop

评论

0赞 Barmar 10/13/2023
缩进已关闭。 并且需要处于相同的缩进级别,并且它们之间的所有代码都需要再缩进一个级别。tryexcept
0赞 Barmar 10/13/2023
不要对 ID、重量和价格使用单独的列表。使用字典列表。{"id": ###, "weight": ###, "price": ###}
0赞 Barmar 10/13/2023
range(len(buyersList))应该是 。 始终为 3。range(len(iD))len(buyersList)
0赞 Rendevouz 10/13/2023
好的,有了这些修正案,它应该按照我的意愿运作吗?
0赞 Barmar 10/13/2023
如果你想要总计,你必须把列表中的所有产品加起来。

答:

0赞 Barmar 10/13/2023 #1

在用户输入重量和价格后,您应该添加 ,而不是将其重新定义为 lambda 函数。然后打印这个变量。w * ptotal

在最后的循环中,您应该遍历 、 和 列表,而不是字典的长度。您可以使用 并行循环访问它们。iDkGproductPricegroceryListzip()

# dictionary to store productID("ID"), price("price")
#with empty list as their values
groceryList ={"productID":[], "weight": [], "price":[]}

# converting dictionary to list for further updation
buyersList = list(groceryList.values())

# variable iD value of "productID" from dictionary 'buyersList'
iD = buyersList[0]

# variable weight value of "quantity" from dictionary 'buyersList'
kG = buyersList[1]

# variable qu value of "price" from dictionary 'a'
productPrice = buyersList[2]

total = 0

# This loop terminates when user select 2.EXIT option when asked
# in try it will ask user for an option as an integer (1 or 2) 
# if correct then proceed with an if/else statement asking for user input

# It should continue and keep adding the total price * weight - BUT THIS FUNCTION DOESN'T WORK

while True:
    try:
        choice = int(input("1.SELECT\n2.EXIT\nPlease select 1 or 2 (from the menu) here : ")) #use new line formatting
        if choice == 1:
            i = int(input("Please enter the product ID here : "))
            # input quantity of product
            w = float(input("Please enter how many KG you would like : "))

            p = float(input("Please enter the price of the product here : "))

            # append value of in "name", "quantity", "price"
            iD.append(i)  
            # as na = b[0] it will append all the value in the
            # list eg: "name":["rice"]
            kG.append(w)
            productPrice.append(p)     

            # after appending new value the sum in price
            # as to be calculated
            total += w * p
            print("\nYour current total amount is", total)

        elif choice == 2:
            break

        else:
            print("Sorry, that option isn't available. Please, try again.")

    except ValueError:
        print("Invalid response. Please try again.")
        
#when user asks to exit loop - their grocery list is printed to screen
print("\n\n\nGROCERY LIST")

for id, weight, price in zip(iD, kG, productPrice):
    print(id, weight, price)