Python装机选购程序实现已选商品小计与总价计算
PC配件选购程序选品存储与总价计算实现
原有代码问题
- 原
choice函数仅完成选项展示、用户输入和选择提示,未返回选中的商品信息,选品数据没有留存 - 未设置专门的存储结构保存所有已选商品,无法统一汇总输出条目、计算总价
- 之前测试用的字典同key值累加逻辑,不适合存储独立的商品条目,存储多件已选商品用列表结构更适配
- 原代码输入校验存在笔误,校验规则包含菜单不存在的选项8,且没有对用户输入的非数字、不存在的选项序号做拦截,容易触发报错
原有参考代码
测试字典累加逻辑代码
dict1 = [{'ca':2, 'na':3},{'ca':3, 'na':4}] final = {} for d in dict1: for k in d.keys(): final[k] = final.get(k,0) + d[k] print (final)
未完成的业务代码
cpu = { 1: ("AMD Ryzen 5 5600X", 190), 2: ("AMD Ryzen 7 5800X", 260), 3: ("AMD Ryzen 9 5900X", 350), 4: ("AMD Ryzen 9 5950X", 500), 5: ("Intel Core i5-12400F", 135), 6: ("Intel Core i7-12700K", 350), 7: ("Intel Core i9-12900k", 500) } gpu={ 1: ("Nvidia Geforce RTX 3050", 300), 2: ("Nvidia Geforce RTX 3060", 400), 3: ("Nvidia Geforce RTX 3070", 600), 4: ("Nvidia Geforce RTX 3080", 900), 5: ("Nvidia Geforce RTX 3090", 1500) } store = { 1: ("Samsung 970 EVO plus", 100), 2: ("Samsung 980 PRO", 300), 3: ("Seagate Barracuda Compute", 50), 4: ("Kingston A400", 25), 5: ("Seagate Ironwolf PRO", 500) } ram = { 1: ("Crucial BallistiX", 40), 2: ("Corsair Vengance Pro RGB", 80), 3: ("Tiden Z RGB", 152), 4: ("G.Skills Ripjaws V", 262), 5: ("ADATA XPG LANCER RGB", 300) } cooler = { 1: ("NZXT Kraken X53", 100), 2: ("Noctua NHD-15", 90), 3: ("ASUS ROG StiX", 240), } cases = { 1: ("NZXT H510 ELITE", 150), 2: ("Lian LI 011 Dynamic", 208), 3: ("Corsair Carbide", 50) } def choice( menu ): for k,v in menu.items(): print( f"{k}: {v[0]} £{v[1]}" ) val = int(input("Make your choice by number:" )) print( f"You have chosen the {menu[val][0]} for £{menu[val][1]}" ) menu_choice=input("What would you like to buy today? [1: CPU's] [2: GPU's] [3: Storage] [4: RAM] [5: CPU Cooler] [6: Cases] [7: One of each] [Type the number that you want.]: ") if menu_choice not in "12345678": print("You did not choose something or put in the wrong number.") exit() if menu_choice in "17": print("Choose your CPU.:") c = choice(cpu) if menu_choice in "27": print("GPU's, press enter to go to the next product:") c = choice(gpu) if menu_choice in "37": print("Storage, press enter to go to the next product:") c = choice(store) if menu_choice in "47": print("RAM, press enter to go to the next product:") c = choice(ram) if menu_choice in "57": c = choice(cooler) if menu_choice in "67": c = choice(cases)
修改后可实现需求的完整代码
核心调整逻辑:
- 给
choice函数增加返回值,返回用户选中的(商品名,价格)元组,补全输入合法性校验,避免非法输入触发程序崩溃 - 初始化
selected_items空列表,每次选完商品就把返回的商品信息追加到列表中 - 所有选品流程结束后,遍历列表打印全量已选商品,累加计算总价格并输出
cpu = { 1: ("AMD Ryzen 5 5600X", 190), 2: ("AMD Ryzen 7 5800X", 260), 3: ("AMD Ryzen 9 5900X", 350), 4: ("AMD Ryzen 9 5950X", 500), 5: ("Intel Core i5-12400F", 135), 6: ("Intel Core i7-12700K", 350), 7: ("Intel Core i9-12900k", 500) } gpu={ 1: ("Nvidia Geforce RTX 3050", 300), 2: ("Nvidia Geforce RTX 3060", 400), 3: ("Nvidia Geforce RTX 3070", 600), 4: ("Nvidia Geforce RTX 3080", 900), 5: ("Nvidia Geforce RTX 3090", 1500) } store = { 1: ("Samsung 970 EVO plus", 100), 2: ("Samsung 980 PRO", 300), 3: ("Seagate Barracuda Compute", 50), 4: ("Kingston A400", 25), 5: ("Seagate Ironwolf PRO", 500) } ram = { 1: ("Crucial BallistiX", 40), 2: ("Corsair Vengance Pro RGB", 80), 3: ("Tiden Z RGB", 152), 4: ("G.Skills Ripjaws V", 262), 5: ("ADATA XPG LANCER RGB", 300) } cooler = { 1: ("NZXT Kraken X53", 100), 2: ("Noctua NHD-15", 90), 3: ("ASUS ROG StiX", 240), } cases = { 1: ("NZXT H510 ELITE", 150), 2: ("Lian LI 011 Dynamic", 208), 3: ("Corsair Carbide", 50) } def choice(menu): for k,v in menu.items(): print(f"{k}: {v[0]} £{v[1]}") while True: try: val = int(input("Make your choice by number: ")) if val not in menu.keys(): print("Invalid option, please enter a number from the list above.") continue break except ValueError: print("Please enter a valid number.") selected = menu[val] print(f"You have chosen the {selected[0]} for £{selected[1]}") return selected selected_items = [] total_price = 0 menu_choice = input("What would you like to buy today? [1: CPU's] [2: GPU's] [3: Storage] [4: RAM] [5: CPU Cooler] [6: Cases] [7: One of each] [Type the number that you want.]: ") if menu_choice not in "1234567": print("You did not choose something or put in the wrong number.") exit() menu_choice = int(menu_choice) if menu_choice in (1,7): print("Choose your CPU.:") selected_items.append(choice(cpu)) if menu_choice in (2,7): print("GPU's, press enter to go to the next product:") selected_items.append(choice(gpu)) if menu_choice in (3,7): print("Storage, press enter to go to the next product:") selected_items.append(choice(store)) if menu_choice in (4,7): print("RAM, press enter to go to the next product:") selected_items.append(choice(ram)) if menu_choice in (5,7): print("Choose your CPU cooler:") selected_items.append(choice(cooler)) if menu_choice in (6,7): print("Choose your case:") selected_items.append(choice(cases)) print("\n" + "="*30) print("Your selected items:") for item_name, item_price in selected_items: print(f"- {item_name}: £{item_price}") total_price += item_price print("="*30) print(f"*Total cost: £{total_price}*")
运行效果
输入7选择全品类各选一件模式时,程序会依次引导用户选择每个品类的配件,所有选择完成后自动列出全部选中的商品条目,最后输出合计总价。单独选择某一个品类时,也会正常输出对应选品和价格。
内容的提问来源于stack exchange,提问作者MaxStrongman
相关产品推荐
相关产品推荐

