为什么我的Python代码只会累加购物车中最后一个物品的价格?

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

Why does my Python code only add up the price of the last item in my shopping cart?

问题

Here's the translated code:

shopping_cart = []

food = shopping_cart

# 库存
stock = {
    "\n水果\n": {
        "香蕉": 50,
        "苹果": 15,
        "橙子": 32,
        "梨": 15,
    },

    "\n饮料\n": {
        "苹果汁": 10,
        "橙汁": 10,
        "菠萝汁": 10,
        "牛奶": 15,
    },

    "\n零食\n": {
        "薯片": 20,
        "饼干": 20,
        "巧克力": 25,
        "糖果": 50,
    }
}

# 价格
prices = {
    # 水果
    "香蕉": 10,
    "苹果": 5,
    "橙子": 5,
    "梨": 5,

    # 饮料
    "苹果汁": 5,
    "橙汁": 5,
    "菠萝汁": 5,
    "牛奶": 4.80,

    # 零食
    "薯片": 7,
    "饼干": 4,
    "巧克力": 6,
    "糖果": 5.50
}

# 循环
while True:
    item = input("请输入商品: ").lower()
    shopping_cart.append(item)

    if item == 'done':
        break

    if item == 'cart':
        print(shopping_cart)

    if item == 'stock':
        for category, items in stock.items():
            print(category)
            for item, quantity in items.items():
                print(item + ":", quantity)


# 定义账单变量
def shopping_bill(food):
    total = 0
    for item in food:
        if item in prices:
            total += prices[item]
    print("您的账单总额为 $", total)
    print("感谢您在B&K超市购物!")

shopping_bill(food)

This code appears to be designed for a shopping cart program. It lets you add items to your cart and calculate the total price when you type "done." The translated code should work similarly to the original Python code.

英文:

So I am trying to make a python code that lets you add items to your shopping cart then once you type "done" it is supposed to add up the prices of each item you added to your shopping cart and then tell you the total price.

My issue is that once I type "done" it only adds up the price of the last item that I added to my shopping cart then prints it out.

shopping_cart = [""]
food = shopping_cart
#Stock
stock = {
"\nFruit\n\n"
"banana": 50,
"apple": 15,
"orange": 32,
"pear": 15,
"\nDrinks\n\n"
"apple juice": 10,
"orange juice": 10,
"pinapple juice": 10,
"milk": 15,
"\nSnacks\n\n"
"chips": 20,
"cookies": 20,
"chocolate": 25,
"lollies": 50
}
#Prices 
prices = {
#Fruit
"banana": 10,
"apple": 5,
"orange": 5,
"pear": 5,
#Drinks
"apple juice": 5,
"orange juice": 5,
"pinapple juice": 5,
"milk": 4.80,
#Snacks
"chips": 7,
"cookies": 4,
"chocolate": 6,
"lollies": 5.50
}
#Loop
while True:
item = input("\nEnter an item: ").lower()
shopping_cart.append(item)
if item == 'done':
break
if item == 'cart':
print(shopping_cart)
if item == 'stock':
for k, v in stock.items():
print(k + ":", v)
#Define the bill variable
def shopping_bill(food):
total = 0
for key in prices:
for key in stock:
for item in food:
if item == key in prices:
total += prices[key]
break 
print("Your bill total equals $", total)
print("Thank you for shopping at B&K Supermarket!")
shopping_bill(food)

答案1

得分: 2

这里存在两个主要问题。第一个问题是在用户输入购物清单之前,你将食物定义为 shopping_cart,因此它保持为空列表。

要解决这个问题,将 food = shopping_cart 移动到 while True 循环之后,像这样:

while True:
    item = input("Enter an item: ").lower()
    shopping_cart.append(item)

    if item == 'done':
        break

    if item == 'cart':
        print(shopping_cart)

    if item == 'stock':
        for k, v in stock.items():
            print(k + ":", v)

food = shopping_cart

第二个问题是你正在将类似 "\nFruit\n\n" 等内容添加到顶部的字典中。你需要将这些注释掉,以便代码不认为它们是字典的一部分。

stock = {
    # 水果
    "banana": 50,
    "apple": 15,
    "orange": 32,
    "pear": 15,

    # 饮料
    "apple juice": 10,
    "orange juice": 10,
    "pineapple juice": 10,
    "milk": 15,

    "chips": 20,
    "cookies": 20,
    "chocolate": 25,
    "lollies": 50
}

将上述代码更改后,通过以下方式将其输入到提示符中:

> banana
> apple
> done

将会产生以下结果:

Your bill total equals $ 15
Thank you for shopping at B&K Supermarket!
英文:

There are two main problems here. The first is that you're defining food as shopping_cart before the user has entered their shopping list. Thus, it remains an empty list.

To fix this, move food = shopping_cart to after the while True loop, like so:

while True:
item = input("\nEnter an item: ").lower()
shopping_cart.append(item)
if item == 'done':
break
if item == 'cart':
print(shopping_cart)
if item == 'stock':
for k, v in stock.items():
print(k + ":", v)
food = shopping_cart

The second problem is that you're adding "\nFruit\n\n" and similar things to the dictionary up the top. You need to comment these out so the code doesn't think they're parts of the dictionary.

stock = {
#fruit
"banana": 50,
"apple": 15,
"orange": 32,
"pear": 15,
#drinks
"apple juice": 10,
"orange juice": 10,
"pinapple juice": 10,
"milk": 15,
"chips": 20,
"cookies": 20,
"chocolate": 25,
"lollies": 50
}

Entering this into the prompt with the code changes above

> banana
> apple
> done

produces

Your bill total equals $ 15
Thank you for shopping at B&K Supermarket!

答案2

得分: 1

大部分与其他答案相同,但检查股票不会降至0以下。

shopping_cart = []

food = shopping_cart

#库存
stock = {
    #水果
    "banana": 50,
    "apple": 15,
    "orange": 32,
    "pear": 15,

    #饮料
    "apple juice": 10,
    "orange juice": 10,
    "pinapple juice": 10,
    "milk": 15,

    #零食
    "chips": 20,
    "cookies": 20,
    "chocolate": 25,
    "lollies": 50
}

#价格
prices = {
    #水果
    "banana": 10,
    "apple": 5,
    "orange": 5,
    "pear": 5,

    #饮料
    "apple juice": 5,
    "orange juice": 5,
    "pinapple juice": 5,
    "milk": 4.80,

    #零食
    "chips": 7,
    "cookies": 4,
    "chocolate": 6,
    "lollies": 5.50
    
}

#定义账单变量
def shopping_bill(food):
    total = 0
    for key in stock:
       total += food.count(key) * prices[key]
    print("您的账单总计为 $", total)
    print("感谢您光临 B&K 超市!")

#循环
while True:
    item = input("\n输入一个物品: ").lower()


    if item == 'done':
        shopping_bill(shopping_cart)
        break

    if item == 'cart':
        print(shopping_cart)
        continue

    if item == 'stock':
        for k, v in stock.items():
            print(k + ":", v)
        continue

    for key in stock:
       if item == key:
              if stock[item] > 0:
                     shopping_cart.append(item)  
                     stock[item] -= 1
                     print("已添加到购物车")
              else:
                     print("缺货") 
英文:

Mostly the same as the other answer but checks that the stock doesn't go below 0.

shopping_cart = []
food = shopping_cart
#Stock
stock = {
#Fruit
"banana": 50,
"apple": 15,
"orange": 32,
"pear": 15,
#Drinks
"apple juice": 10,
"orange juice": 10,
"pinapple juice": 10,
"milk": 15,
#Snacks
"chips": 20,
"cookies": 20,
"chocolate": 25,
"lollies": 50
}
#Prices 
prices = {
#Fruit
"banana": 10,
"apple": 5,
"orange": 5,
"pear": 5,
#Drinks
"apple juice": 5,
"orange juice": 5,
"pinapple juice": 5,
"milk": 4.80,
#Snacks
"chips": 7,
"cookies": 4,
"chocolate": 6,
"lollies": 5.50
}
#Define the bill variable
def shopping_bill(food):
total = 0
for key in stock:
total += food.count(key) * prices[key]
print("Your bill total equals $", total)
print("Thank you for shopping at B&K Supermarket!")
#Loop
while True:
item = input("\nEnter an item: ").lower()
if item == 'done':
shopping_bill(shopping_cart)
break
if item == 'cart':
print(shopping_cart)
next
if item == 'stock':
for k, v in stock.items():
print(k + ":", v)
next
for key in stock:
if item == key:
if stock[item] > 0:
shopping_cart.append(item)  
stock[item] -= 1
print("added to cart")
else:
print("out of stock") 

huangapple
  • 本文由 发表于 2023年5月30日 08:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360952.html
匿名

发表评论

匿名网友

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

确定