I am creating a simple queue system with Python, however there is just one problem: I have coded yet the terminal just does not show the results I need. Maybe a problem with the coding?
The image shown here is what I got: enter image description here
Here is the Python code I've used:
# code
import queue
from time import sleep
from tkinter import Menu
ticket = 2000
ticket_in_queue = []
table_assignment = {
'Table 1': 'Not assigned',
'Table 2': 'Not assigned',
'Table 3': 'Not assigned',
'Table 4': 'Not assigned',
}
def add_ticket(ticket):
ticket_in_queue.append(ticket)
return ticket_in_queue
# message
print("Enter 0 to 5 for following options:")
print("0 -> Issue new ticket number")
print("1 -> Assign first ticket in queue in Table 1")
print("2 -> Assign first ticket in queue in Table 2")
print("3 -> Assign first ticket in queue in Table 3")
print("4 -> Assign first ticket in queue in Table 4")
print("5 -> Quit program")
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
option = " "
while option != "exit":
option = input("Enter your option: ")
if option == "0":
ticket = 1
add_ticket(ticket)
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "1":
table_assignment['Table 1'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "2":
table_assignment['Table 2'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "3":
table_assignment['Table 3'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "4":
table_assignment['Table 4'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "5":
print("Quitting program...")
quit()
else:
print("Invalid option")
Any assistance will be appreciated. Thank you.
CodePudding user response:
Because everything after the while loop is outside of its scope. the solution just tab everything inside the while loop.
# code
import queue
from time import sleep
from tkinter import Menu
ticket = 2000
ticket_in_queue = []
table_assignment = {
'Table 1': 'Not assigned',
'Table 2': 'Not assigned',
'Table 3': 'Not assigned',
'Table 4': 'Not assigned',
}
def add_ticket(ticket):
ticket_in_queue.append(ticket)
return ticket_in_queue
# message
print("Enter 0 to 5 for following options:")
print("0 -> Issue new ticket number")
print("1 -> Assign first ticket in queue in Table 1")
print("2 -> Assign first ticket in queue in Table 2")
print("3 -> Assign first ticket in queue in Table 3")
print("4 -> Assign first ticket in queue in Table 4")
print("5 -> Quit program")
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
option = ""
while option != "exit":
option = input("Enter your option: ")
if option == "0":
ticket = 1
add_ticket(ticket)
print(ticket_in_queue)
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "1":
table_assignment['Table 1'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "2":
table_assignment['Table 2'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "3":
table_assignment['Table 3'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "4":
table_assignment['Table 4'] = ticket
print("Tickets in queue:", ticket_in_queue)
print("Table assignment:",table_assignment)
elif option == "5":
print("Quitting program...")
quit()
else:
print("Invalid option")
CodePudding user response:
there is logical error in your code
option = " "
while option != "exit":
option = input("Enter your option: ")
there is a loop which will continue to execute as long as it doesnt get "exit" as input and it overwrites whatever you put in the option variable before. you need to fix this logic. if you want to save multiple options then use an array or add indentation to your if blocks to make them part of while loop and if you want to get just one input then dont use this while loop there is no need for this you can end execution in your else this is your output when we run it
CodePudding user response:
You have to use the if else statements in the while loop, Therefore you will have to give indentation to the if else statements.