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

Python队列代码菜单重复询问问题的排查与修复请求

队列功能Python代码的菜单重复问题及修复方案

问题情况

实现队列功能的Python代码运行时,菜单会重复多次询问操作选择,比如选择Push时需要多次输入选择才能进入对应逻辑,运行示例如下:

What do you want to do:
1)Push
2)Pop
3)Display
4)Quit
Make a selection: 1
What do you want to do:
1)Push
2)Pop
3)Display
4)Quit
Make a selection: 1
What do you want to do:
1)Push
2)Pop
3)Display
4)Quit
Make a selection: 1
You choose: Push
How many items do you want to enter:

原代码如下:

# creating menu
def menu():
    print("What do you want to do:")
    print("1)Push")
    print("2)Pop")
    print("3)Display")
    print("4)Quit")
    choice = int(input("Make a selection: "))
    return choice


# creating a queue with a list
def create_queue():

    # creating a queue
    queue = []
    while menu() > 0 & menu() < 5:
        if menu() == 1:
            print("You choose: Push")
            num_input = int(input("How many items do you want to enter: "))
            for i in range(num_input):
                queue.append(input("Enter items: "))
        elif menu() == 2:
            print("You choose: Pop")
            # making sure queue is not empty
            if len(queue) == 0:
                print("Empty, nothing to get rid of.")
                return
            else:
                print("Popping item out")
                queue.pop(0)
        elif menu() == 3:
            print("You choose: Display")
            print(queue)
        elif menu() == 4:
            print("You chose: Quit")
            return
        else:
            print("Not a choice")
            return


create_queue()

问题原因

  1. 重复调用menu()函数:原代码在while循环条件、if/elif判断里多次调用menu(),每调用一次就会弹出菜单让用户输入,导致用户需要多次输入才能进入对应操作逻辑。
  2. 循环条件运算符错误:使用了按位与运算符&,应替换为逻辑与运算符and,不过这不是核心问题。
  3. 错误使用return:在Pop操作的空队列判断里用了return,会直接退出create_queue函数,无法回到菜单循环。

修复后的代码

# creating menu
def menu():
    print("What do you want to do:")
    print("1)Push")
    print("2)Pop")
    print("3)Display")
    print("4)Quit")
    choice = int(input("Make a selection: "))
    return choice


# creating a queue with a list
def create_queue():
    queue = []
    while True:
        # 只调用一次menu(),将选择结果存入变量
        choice = menu()
        if choice == 1:
            print("You choose: Push")
            num_input = int(input("How many items do you want to enter: "))
            for i in range(num_input):
                queue.append(input("Enter items: "))
        elif choice == 2:
            print("You choose: Pop")
            if len(queue) == 0:
                print("Empty, nothing to get rid of.")
                # 回到循环开头,不退出函数
                continue
            else:
                print("Popping item out")
                queue.pop(0)
        elif choice == 3:
            print("You choose: Display")
            print(queue)
        elif choice == 4:
            print("You chose: Quit")
            # 退出循环,结束函数
            break
        else:
            print("Not a valid choice, please try again.")


create_queue()

关键改动说明

  • 循环内部仅调用一次menu(),将用户选择存入choice变量,所有判断基于该变量,避免重复弹出菜单。
  • 改用while True循环,通过break在选择Quit时退出循环,保证操作完成后能回到菜单。
  • 移除空队列判断里的return,改用continue回到循环开头,继续显示菜单。
  • 对无效选择给出提示后,自动回到菜单循环。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:15:36