Do while loop in python

I am trying to implement the queue data structure in Python. I want to make the program function according to user-input. Like, the user should be able to decide if they want to enqueue or dequeue elements, or display elements in the queue. I was wondering if there is an alternative to the do-while loop and the switch case statement in C++. If anybody can suggest anything, it would be very helpful!

Is the ‘do while’ loop not working for what you are attempting to do?
Do you have examples of the code you are writing?

1 Like

Hi!

I have worked a little bit on your question and I hope that I have understood it.

I have the following solution that could be improved. Error handling has not been considered!

Please check if that what you expect!

def enqueue(liste):
    item = int(input("item "))
    liste.append(item)
  
def dequeue(liste):
    liste.pop(0)
  
def display(liste):
    print(liste)
  
switcher = {
        0: enqueue,
        1: dequeue,
        2: display
    }
    
  
def queue(liste):
    argument = eval(input("Enter your choice: "))
    op = switcher.get(argument)
    
    while (argument == 0 or argument == 1 or argument == 2):
        op(liste)
        argument = eval(input("Enter your choice: "))
        op = switcher.get(argument)

I hope it works for you!

1 Like

Thank you for the solution! I copy pasted your code, and just added a line to call the function. And, it worked! But could you please explain to me the part after the switcher, as it would be helpful!
Thanks again!

1 Like

It did’nt work for me.

Create an empty list. Add the function call at the end with the name of the empty list as a parameter. It should work then, as it did for me.

def enqueue(liste):
    item = int(input("item "))
    liste.append(item)
  
def dequeue(liste):
    liste.pop(0)
  
def display(liste):
    print(liste)
  
switcher = {
        0: enqueue,
        1: dequeue,
        2: display
    }
    
  
def queue(liste):
    argument = eval(input("Enter your choice: "))
    op = switcher.get(argument)
    
    while (argument == 0 or argument == 1 or argument == 2):
        op(liste)
        argument = eval(input("Enter your choice: "))
        op = switcher.get(argument)

my_list = []
queue(my_list)

Hi
Do you mean def queue(liste)... ?
If yes:

  1. the function queue is to to manipulate the list (append(), pop() and so on?
  2. “argument” is an item of the dictionary switcher and it is placed just before while because this needs initialization.
  3. “op” is to look if the argument exists in switcher.
  4. while is create and manipulate the list as long as you wish.
  5. op(liste) is similar to case in other programming languages. Here the argument will be applied on the list you have created.
  6. the two last statements in while are similar to the incrementation (i++) in other programming languages.

I hope it could help you to understand the similarity to other programming languages!

I have worked with the while testing! I created a list = [1, 2, 3, …] and checked the code directly.

Thanks a lot for the explanation! I understood the concept now!! :pray:t2: :grinning:

1 Like