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

如何解决Python购物程序中while循环跳转至旧输入而非最新输入的问题

问题原因
  • 你在all_items函数内部定义的items是局部变量,仅在函数内部生效,修改后不会同步到外层while循环用来做判断的全局items变量。所以你哪怕在函数里输入了新的内容,外层循环读取的还是上一次的旧值,自然就会跳回之前的分区。
  • 你把all_items函数的定义写在了while循环内部,每次循环都会重复定义函数,属于不必要的冗余写法。
修复方案

调整逻辑:把函数移到循环外,让函数返回用户输入的内容,每次调用函数时把返回值赋值给外层的items变量,保证判断用的都是最新输入。修复后的完整代码如下:

x = 50 # 大额预算门槛
y = 5 # 小额预算门槛
# 商品价格
# 甜点类
candy_bar = 1
candy_bag = 2
ice_cream = 2

# 定义获取商品分类输入的函数,移到循环外,返回用户输入
def get_category_input():
    return input("你想要购买乳制品、肉类、蔬菜、水果还是甜点?如果不想购买任何商品请输入“Nothing”:")

# 门店欢迎语
print("你好呀!欢迎来到普通大餐杂货店,这里的店就是你的店!;)")

# 获取用户预算
budget = int(input("请问你今天的预算是多少?"))
if budget <= y:
    print("你最多只能买5根棒棒糖,钱不够哦!去街对面的清仓店吧!")
    print("**你转身离开前往清仓店...")
    exit()
elif budget <= x:
    print("你大概能买点东西...但不够买一顿大餐哦!你或许可以去普通中餐杂货店看看!")
    print("**你转身离开前往普通中餐杂货店...")
    exit()
else:
    print("太棒了!我们这里有很多食材可以做大餐,甚至超级大份的那种!")
    print("**拉着你的手带你走进漂亮的普通大餐杂货店...")

# 首次获取分类输入
items = get_category_input()
while True:
    if items == "Nothing":
        bye = input("你确定什么都不买吗?请输入Yes或No:")
        if bye == "Yes":
            print("感谢你光临普通大餐杂货店!!!")
            exit()
        elif bye == "No":
            items = get_category_input()
    elif items == "desserts":
        print("我们的甜点如下:\n1. 棒棒糖:1美元\n2. 袋装糖果:2美元\n3. 冰淇淋:2美元")
        dessert_choice = input("你想要买哪款甜点?如果什么都不想买请输入“Nothing”:")
        if dessert_choice == "Candy Bar":
            confirm = input("你确定要买这个吗?请输入Yes或No:")
            if confirm == "Yes":
                budget -= candy_bar
                print("你当前剩余预算:", budget, "美元")
                # 购买完成后返回分类选择
                items = get_category_input()
            elif confirm == "No":
                items = get_category_input()
        elif dessert_choice == "Candy Bag":
            confirm = input("你确定要买这个吗?请输入Yes或No:")
            if confirm == "Yes":
                budget -= candy_bag
                print("你当前剩余预算:", budget, "美元")
                items = get_category_input()
            elif confirm == "No":
                items = get_category_input()
        elif dessert_choice == "Ice Cream":
            confirm = input("你确定要买这个吗?请输入Yes或No:")
            if confirm == "Yes":
                budget -= ice_cream
                print("你当前剩余预算:", budget, "美元")
                items = get_category_input()
            elif confirm == "No":
                items = get_category_input()
        elif dessert_choice == "Nothing":
            confirm = input("你确定什么甜点都不买吗?请输入Yes或No:")
            if confirm == "Yes":
                items = get_category_input()
            elif confirm == "No":
                continue
    else:
        print("嗯...我们店里没有这个品类...")
        items = get_category_input()
额外优化建议
  • 当前原始代码购买商品后没有修改budget变量的值,多次购买时每次扣减的都是初始预算,上述修复代码已经补充了预算更新逻辑。
  • 可以对用户输入做大小写统一、空格剔除处理,避免用户输入大小写不符(比如nothing、Desserts)、前后带空格导致识别失败的问题。
  • 相同逻辑的确认提示可以提取成公共函数,减少重复代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 00:45:00