You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3食堂售卖程序缺货提示逻辑异常问题咨询

食堂售卖程序缺货提示修复方案

问题现象

当餐品达到售罄阈值后,再次输入该餐品无法重复触发缺货提示。例如输入3次"Vegetar"触发Sorry, no more left of 'Vegetar' today.提示后,后续再次输入"Vegetar"时,程序仅输出询问语句,不再提示缺货。

问题根源

  1. 依赖销量累计值判断售罄,触发售罄后会将对应销量sum重置为0,导致后续输入又会重新累加sum,不再满足触发条件
  2. 售罄后未禁止售卖操作,仍然会执行正常售卖的累加逻辑,进一步打乱状态判断

修复方案

新增售罄状态变量跟踪每个餐品是否售罄,修改售卖逻辑先检查售罄状态,调整售罄触发逻辑为标记状态而非重置销量。

修改后的完整代码

items = ['Dagens', 'Vegetar', 'Halal']
prices = [53.90, 42.50, 59.90]
number = [1, 1, 1]
# 新增售罄状态标记,对应三个餐品是否已售罄
sold_out = [False, False, False]

meal_sum = 0
total_sum = 0
dagens_sum = 0
vegetar_sum = 0
halal_sum = 0

def sell(item):
    global meal_sum
    global total_sum
    global dagens_sum
    global vegetar_sum
    global halal_sum
    
    # 先检查餐品是否在售且未售罄
    if item not in items:
        print(f"{item} is not on the menu!\n")
        return
    
    item_idx = items.index(item)
    if sold_out[item_idx]:
        print(f"Sorry, no more left of '{item}' today.\n")
        return
    
    # 执行正常售卖逻辑
    if item == 'Dagens':
        meal_sum += number[0]
        total_sum += prices[0]
        dagens_sum += number[0]
        print("Dagens, here you go.\n")
    elif item == 'Vegetar':
        meal_sum += number[1]
        total_sum += prices[1]
        vegetar_sum += number[1]
        print("Vegetar, here you go.\n")
    elif item == 'Halal':
        meal_sum += number[2]
        total_sum += prices[2]
        halal_sum += number[2]
        print("Halal, here you go.\n")

def mark_sold_out(item_idx, item_name):
    global sold_out
    sold_out[item_idx] = True
    print(f"Sorry, no more left of '{item_name}' today.")

def empty_cash_registry():
    global meal_sum
    meal_sum = 0
    print("Cash registry emptied. Thieves be warned!\n")

def cafeteria():
    global meal_sum
    global total_sum
    print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
    user_input = input("> ")
    while user_input != 'not hungry':
        question = 0
        if user_input == 'how is business?':
            question = 1
            if total_sum == 0:
                print("Not good so far. No one seems to be hungry today!\n")
            elif total_sum < 500:
                print("Alright. Could have been better\n")
            elif total_sum > 500:
                print("Excellent! Lots of hungry students around today.\n")
        if question == 0:
            sell(user_input)
        # 检查是否达到售罄阈值,标记售罄状态
        if dagens_sum > 15 and not sold_out[0]:
            mark_sold_out(0, 'Dagens')
        if vegetar_sum > 2 and not sold_out[1]:
            mark_sold_out(1, 'Vegetar')
        if halal_sum > 5 and not sold_out[2]:
            mark_sold_out(2, 'Halal')
        if meal_sum > 200:
            empty_cash_registry()
        print("What would you like today? (type 'how is business?' to attempt small talk, or 'not hungry' to stop)")
        user_input = input("> ")
    print("Welcome back later!\n")

if __name__ == '__main__':
    cafeteria()

关键修改点

  • 新增sold_out列表,记录每个餐品的售罄状态
  • sell()函数新增前置检查:先判断餐品是否在售,再判断是否售罄,售罄则直接提示
  • 将原有的餐品售罄函数合并为通用的mark_sold_out(),仅标记售罄状态,不再重置销量sum
  • 售罄触发逻辑增加not sold_out[item_idx]判断,避免重复标记

内容的提问来源于stack exchange,提问作者codeb_s

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 12:10:54