Python购物车代码优化:如何仅用外层while循环实现功能
购物车项目优化思路(单外层循环实现)
我是Python及编程领域的新手,正在开发第一个迷你项目——购物车。目前代码可正常运行,但有人告知我可以仅通过外层while循环实现全部逻辑,无需第二层循环。我一整天尝试调整却毫无进展,希望能得到实现思路的指导,谢谢。
原代码
shopping_list = [] print("Hi, Welcome to Jolly's Market.") while True: customer = input("To add to the shopping cart, press 1. To checkout and leave press 2.\n") if customer == "1": print("To return to the menu, type exit . To remove items, type r") while customer != "exit" or customer != "r": shopping_list.append(input("Add to cart: ")) print(shopping_list) customer = input("").lower() if customer == "exit": print("Sending you back to the menu") break if customer == "r": shopping_list.pop(int(input("Remove item "))) print(shopping_list) shopping_list.append(input("Add to cart: ")) print(shopping_list) customer = input("").lower() if len(shopping_list) == 10: print("You have ten items, do you wish to add more? (y, n)") customer = input(" ").lower() if customer == "y": shopping_list.append(input("Add to cart: ")) elif customer == "n": print("Sending you back to the main menu") break
单外层循环实现思路
核心是用状态变量追踪当前程序所处的阶段(主菜单/添加商品),把原本第二层循环的逻辑拆分到外层循环的不同分支中:
- 定义一个状态变量,比如
current_mode,初始值设为"main_menu",用来标记当前是在主菜单还是添加商品的流程里。 - 外层循环始终运行,每次根据
current_mode的值执行对应逻辑:- 主菜单状态:显示1(添加商品)和2(结账离开)的选项。用户选1就把
current_mode改成"adding_items";选2就直接退出循环结束程序。 - 添加商品状态:处理添加、删除、返回主菜单的操作:
- 先提示用户可以输入商品名添加,或输入
exit返回菜单、r删除商品。 - 接收用户输入后分情况处理:
- 输入
exit:打印提示,把current_mode改回"main_menu",回到主菜单逻辑。 - 输入
r:执行删除操作(注意加简单判断避免索引越界),删除后保持current_mode不变,继续停留在添加流程。 - 输入商品名:添加到购物车,然后检查购物车长度:
- 如果长度到10,询问是否继续添加。用户选
n就改回主菜单状态;选y就继续允许添加。
- 如果长度到10,询问是否继续添加。用户选
- 输入
- 所有操作完成后,外层循环会再次执行,根据更新后的
current_mode进入对应分支,无需嵌套循环。
- 先提示用户可以输入商品名添加,或输入
- 主菜单状态:显示1(添加商品)和2(结账离开)的选项。用户选1就把
调整后的示例代码
shopping_list = [] current_mode = "main_menu" # 状态变量:main_menu 或 adding_items print("Hi, Welcome to Jolly's Market.") while True: if current_mode == "main_menu": customer_choice = input("To add to the shopping cart, press 1. To checkout and leave press 2.\n") if customer_choice == "1": current_mode = "adding_items" print("To return to the menu, type exit. To remove items, type r") elif customer_choice == "2": print("Checkout complete. Thank you for shopping!") break # 退出外层循环,结束程序 else: print("Invalid input, please enter 1 or 2.") elif current_mode == "adding_items": user_input = input("Add to cart: ").lower() if user_input == "exit": print("Sending you back to the menu") current_mode = "main_menu" elif user_input == "r": if not shopping_list: print("Cart is empty, nothing to remove.") continue try: item_index = int(input("Enter the index of the item to remove: ")) shopping_list.pop(item_index) print(f"Updated cart: {shopping_list}") except (ValueError, IndexError): print("Invalid index, please try again.") else: # 添加商品到购物车 shopping_list.append(user_input) print(f"Updated cart: {shopping_list}") # 检查购物车长度是否到10 if len(shopping_list) == 10: more_choice = input("You have ten items, do you wish to add more? (y, n) ").lower() if more_choice == "n": print("Sending you back to the main menu") current_mode = "main_menu" elif more_choice != "y": print("Invalid input, staying in add mode.")
内容的提问来源于stack exchange,提问作者Lightjolly
相关产品推荐
相关产品推荐

