如何实现美妆自动售货机多数量商品添加及购物车总价计算?
美妆自动售货机代码改进需求
我正在学习计算机科学入门课程,需要编写基础代码实现一款美妆自动售货机,目前遇到两个问题:
- 允许用户输入指定商品的购买数量(如购买2支lipstick而非1支)
- 计算购物车总价时需考虑商品购买数量(如2支lipstick+1盘eyeshadow总价应为18美元而非10美元)
现有代码可正常运行,但尚未完成结账(选项d)功能,我打算先解决这两个问题再继续开发。我的思路是新增一个字典并使用count+=1来统计商品数量,但不确定具体实现方式。现有代码如下:
makeup_cart = [] makeup = {'lipstick': 8.00, 'highlight': 9.00, 'eyeshadow': 2.00, 'foundation':11.00, 'eyeliner':5.00, 'bronzer':7.00, 'lipliner':7.00,'blush':5.00, 'mascara':8.00, 'lipgloss ': 4.00 } while True: print("\nMenu:") print("a = Display makeup list and prices") print("b = Add a makeup item to cart") print("c = View cart") print("d = Checkout") print("e = Exit") choice = input("Enter your choice (a-e): ") if choice == "a": print(makeup) # prints makeup dictionary elif choice == "b": item = input("Enter a makeup from the menu item to add: ") #prompts user to input for makeup to add to cart makeup_cart.append(item) #adds item to cart print(f"your {item} has been added to the cart ") print (makeup_cart) # prints current cart elif choice == "c": if makeup_cart: print("Makeup Cart:") for item in makeup_cart: #prints items the user added into the cart as a list of items print( item) else: print("The makeup cart is empty") # if user hasnt added anything then the cart will come up empty
解决方案
用字典统计商品数量确实是最合适的方案,下面是修改后的完整代码,同时解决了数量输入、总价计算问题,还补全了结账功能:
# 把购物车改成字典,键是商品名,值是购买数量 makeup_cart = {} makeup = {'lipstick': 8.00, 'highlight': 9.00, 'eyeshadow': 2.00, 'foundation':11.00, 'eyeliner':5.00, 'bronzer':7.00, 'lipliner':7.00,'blush':5.00, 'mascara':8.00, 'lipgloss': 4.00 } while True: print("\nMenu:") print("a = Display makeup list and prices") print("b = Add a makeup item to cart") print("c = View cart") print("d = Checkout") print("e = Exit") choice = input("Enter your choice (a-e): ").lower() if choice == "a": print("\nAvailable Makeup & Prices:") for item, price in makeup.items(): print(f"- {item}: ${price:.2f}") elif choice == "b": item = input("Enter the makeup item name to add: ").lower() # 检查商品是否存在 if item not in makeup: print(f"Sorry, {item} is not in our menu.") continue # 输入购买数量并校验 while True: try: quantity = int(input(f"How many {item} would you like to buy? ")) if quantity <= 0: print("Please enter a positive number.") continue break except ValueError: print("Please enter a valid number.") # 更新购物车字典 if item in makeup_cart: makeup_cart[item] += quantity else: makeup_cart[item] = quantity print(f"Added {quantity} {item}(s) to your cart.") elif choice == "c": if makeup_cart: print("\nYour Makeup Cart:") total = 0 for item, quantity in makeup_cart.items(): price = makeup[item] subtotal = price * quantity total += subtotal print(f"- {item}: {quantity} x ${price:.2f} = ${subtotal:.2f}") print(f"Current Total: ${total:.2f}") else: print("The makeup cart is empty.") elif choice == "d": if not makeup_cart: print("Your cart is empty, nothing to checkout.") continue # 计算总价 total_price = 0 print("\nCheckout Summary:") for item, quantity in makeup_cart.items(): price = makeup[item] subtotal = price * quantity total_price += subtotal print(f"- {item}: {quantity} x ${price:.2f} = ${subtotal:.2f}") print(f"\nFinal Total: ${total_price:.2f}") print("Thank you for your purchase!") # 结账后清空购物车 makeup_cart.clear() elif choice == "e": print("Goodbye!") break else: print("Invalid choice, please enter a letter from a to e.")
关键改动说明
- 购物车结构优化:将
makeup_cart从列表改为字典,直接关联商品名和购买数量,统计和计算更高效 - 数量输入逻辑:添加商品时先校验商品是否存在,再让用户输入数量,同时做了正整数校验,避免无效输入
- 购物车展示升级:查看购物车时不仅显示商品和数量,还实时计算每个商品的小计和当前总价
- 结账功能补全:遍历购物车字典,根据单价计算每个商品的小计和最终总价,结账后清空购物车
- 细节优化:统一了输入的大小写处理,格式化价格为两位小数,让输出更规范
内容的提问来源于stack exchange,提问作者Abi
相关产品推荐
相关产品推荐

