0
# 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()

This is my code above, every time I run it, it would keep asking what I want to do, this is what I mean when it keeps repeating:

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:

I have tried putting user choice in the loop but it would just get stuck in one choice instead of looping back out.

I just want it to ask once and then afterwards, loop back out. Where did I mess up and what can I change to fix this?

  • `while menu() > 0 & menu() < 5:` is surely not doing what you think is doing, maybe `choice = menu()` and then `while choice > 0 & choice < 5:`?? – Ignatius Reilly Nov 25 '22 at 18:33
  • E.g. `if menu() == 1` needs to call `menu` again, it's not going to use the return of the previous call. There's no reason because a function cannot be called more than once and you never asked the program to store the value of the previous call. – Ignatius Reilly Nov 25 '22 at 18:35
  • Unrelated, but you may want to give it a look: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Ignatius Reilly Nov 25 '22 at 18:38

1 Answers1

0

In your code, you are calling the 'menu()' function multiple times.

while menu() > 0 & menu() < 5:
        if menu() == 1:

Save it in a variable:

choice = None
while choice > 0 and choice < 5:
    choice = menu()
    if choice == 1:
# Etc.

Hope this resolves your issue!

Aarav Dave
  • 41
  • 1
  • 11
  • Hey, I tried that already and this is what it gave me: 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: 5 Enter items: dien Enter items: jaden Enter items: 2 Enter items: 1 Enter items: 3 You choose: Push How many items do you want to enter: 2 Enter items: one Enter items: two You choose: Push How many items do you want to enter: 1 Enter items: 5 You choose: Push How many items do you want to enter: - It would keep looping the same choice – notdien Nov 25 '22 at 18:42
  • Hey, I edited my response. Please try that, it was another bug! – Aarav Dave Nov 25 '22 at 18:46
  • Hey Aarav, so I got it to work with choice being 1 instead of 0, for some reason, when its 0, gives me this error: TypeError: unsupported operand type(s) for &: 'int' and 'NoneType', so I changed choice to 1 and it worked like a charm! Thank you so much for your help!!! – notdien Nov 25 '22 at 18:57
  • I figured out why 0 wouldn't work, it's because it's outside the bound of the while loop. – notdien Nov 25 '22 at 19:00