Python购物车开发:如何限制折扣卡仅可应用一次?
问题解决:折扣卡仅能添加一次的限制实现
问题根源
你的代码无法限制重复添加折扣卡,核心原因是存储已添加折扣卡的集合added_discount_cards被定义在while selection == '1'循环内部,每次进入循环时集合都会被重新初始化(变成空集合),导致之前添加的记录完全丢失,自然无法检测到重复操作。此外代码还存在语法错误和缩进问题。
修正后的代码
print("Welcome to the Store!") print("Enter 1 to start registering your grocery") print("Enter 2 to close the self-check in register") def is_valid_string_input(user_input, valid_choices): return user_input in valid_choices # 将已添加折扣卡的集合放在循环外部,保留会话内的记录 added_discount_cards = set() card_names = {'1': 'Pensioner', '2': 'Weekend', '3': 'Green'} selection = input("Please, enter your choice: ") while selection == '1': print("Enter 1 to add a discount card.") print("Enter 2 to register a new item.") print("Enter 3 to compute the total and pay.") user_choice = input("Please, enter your choice: ") if user_choice == '1': print("1. Enter 1, for adding a Pensioner Card") print("2. Enter 2, for adding a Weekend Card") print("3. Enter 3, for adding a Green Card") discount_choice = input("Please, enter your choice: ") # 验证折扣卡输入合法性 if is_valid_string_input(discount_choice, {'1', '2', '3'}): # 检查是否已添加该折扣卡 if discount_choice not in added_discount_cards: added_discount_cards.add(discount_choice) print(f"The {card_names.get(discount_choice, 'unknown')} Card successfully added!") else: # 对应已添加的情况,缩进修正 card_name = card_names.get(discount_choice, 'unknown') print(f"Error: {card_name} Card already added.") else: print("Invalid selection! Please select a Discount Card from the menu.") elif user_choice == '2': # 这里可以添加登记商品的逻辑 print("Registering new item...") elif user_choice == '3': # 这里可以添加计算总价的逻辑 print("Computing total and processing payment...") # 结账后可以重置集合(如果需要每次购物会话独立) added_discount_cards.clear() selection = '2' # 退出当前购物循环 else: print("Invalid choice! Please select from the menu.") # 如果用户没选择结账,重新询问是否继续购物 if selection != '2': selection = input("Enter 1 to continue shopping, 2 to close: ")
关键修改点
- 集合位置调整:将
added_discount_cards = set()和card_names移到while循环外部,确保整个会话内的折扣卡添加记录被保留。 - 语法错误修复:删除未完成的
for循环和无效代码,修正else块的缩进,使其正确对应“检查折扣卡是否已添加”的条件。 - 函数位置优化:将
is_valid_string_input函数移到循环外部,避免重复定义。 - 补充逻辑:添加了
user_choice为2和3的分支处理,以及购物流程的循环控制。
运行效果(符合期望输出)
Welcome to the Store! Enter 1 to start registering your grocery Enter 2 to close the self-check in register Please, enter your choice: 1 Enter 1 to add a discount card. Enter 2 to register a new item. Enter 3 to compute the total and pay. Please, enter your choice: 1 1. Enter 1, for adding a Pensioner Card 2. Enter 2, for adding a Weekend Card 3. Enter 3, for adding a Green Card Please, enter your choice: 1 The Pensioner Card successfully added! Enter 1 to continue shopping, 2 to close: 1 Enter 1 to add a discount card. Enter 2 to register a new item. Enter 3 to compute the total and pay. Please, enter your choice: 1 1. Enter 1, for adding a Pensioner Card 2. Enter 2, for adding a Weekend Card 3. Enter 3, for adding a Green Card Please, enter your choice: 1 Error: Pensioner Card already added. Enter 1 to continue shopping, 2 to close:
内容的提问来源于stack exchange,提问作者Noob
相关产品推荐
相关产品推荐

