Data Structures in Python — Question 10
Back to all questionsWrite a menu-driven Python program using queue to implement movement of shuttlecock in its box.
queue = []
def display_queue():
if not queue:
print("Queue is empty")
else:
print("Current queue:", queue)
def enqueue(item):
queue.append(item)
def dequeue():
if queue:
removed = queue.pop(0)
print("Removed", removed, "from the queue")
else:
print("Queue is empty, cannot remove")
while True:
print("\nMENU:")
print("1. Add movement to the queue")
print("2. Remove movement from the queue")
print("3. Display current queue")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
movement = input("Enter the movement (up/down): ")
enqueue(movement)
print("Added", movement, "to the queue")
elif choice == '2':
dequeue()
elif choice == '3':
display_queue()
elif choice == '4':
print("Exiting program...")
break
else:
print("Invalid choice, please try again")
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 1
Enter the movement (up/down): up
Added up to the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 1
Enter the movement (up/down): down
Added down to the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 3
Current queue: ['up', 'down']
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 2
Removed up from the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 3
Current queue: ['down']
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 4
Exiting program...
queue
=
[]
def
display_queue
():
if
not
queue
:
print
(
"Queue is empty"
)
else
:
print
(
"Current queue:"
,
queue
)
def
enqueue
(
item
):
queue
.
append
(
item
)
def
dequeue
():
if
queue
:
removed
=
queue
.
pop
(
0
)
print
(
"Removed"
,
removed
,
"from the queue"
)
else
:
print
(
"Queue is empty, cannot remove"
)
while
True
:
print
(
"
\n
MENU:"
)
print
(
"1. Add movement to the queue"
)
print
(
"2. Remove movement from the queue"
)
print
(
"3. Display current queue"
)
print
(
"4. Exit"
)
choice
=
input
(
"Enter your choice (1-4): "
)
if
choice
==
'1'
:
movement
=
input
(
"Enter the movement (up/down): "
)
enqueue
(
movement
)
print
(
"Added"
,
movement
,
"to the queue"
)
elif
choice
==
'2'
:
dequeue
()
elif
choice
==
'3'
:
display_queue
()
elif
choice
==
'4'
:
print
(
"Exiting program..."
)
break
else
:
print
(
"Invalid choice, please try again"
)
Output
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 1
Enter the movement (up/down): up
Added up to the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 1
Enter the movement (up/down): down
Added down to the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 3
Current queue: ['up', 'down']
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 2
Removed up from the queue
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 3
Current queue: ['down']
MENU:
1. Add movement to the queue
2. Remove movement from the queue
3. Display current queue
4. Exit
Enter your choice (1-4): 4
Exiting program...