Thomas Allen
Thomas Allen

Reputation: 11

How can I implement an efficient ticket system in a python based cinema program?

The image below is the tickets that are available in my cinema program, does anyone know the best way of getting a user to input the tickets they require and the number of each ticket. I am completely lost. the only way I can think of is by Asking about each individual ticket but that is inefficient.

("""We have a number of ticket options available
-----------------Tickets-----------------
1) Standard : £10.20
2) Child : £7.70
3) Student : £8.20
4) Senior Ticket (60+) : £6.80
-----------------------------------------""")
Tickets, number = input("Please enter the number corresponding to your ticket choice followed by the number of tickets you would like to purchase: ").split()
choice = input("Would you like to purchase more tickets?" )
if choice == "yes":
   Tickets1 = input("Please enter the number corresponding to your additional ticket choice followed by the number of tickets you would like to purchase: ").split()

Upvotes: 1

Views: 677

Answers (2)

Alexander
Alexander

Reputation: 17345

I would just stick the ticket request logic in a loop that keeps running until the user says they they are finished. Like so:

print("""We have a number of ticket options available
-----------------Tickets-----------------
1) Standard : £10.20
2) Child : £7.70
3) Student : £8.20
4) Senior Ticket (60+) : £6.80
-----------------------------------------""")

def get_ticket_order():
    complete = False
    order = []
    while not complete:
        Tickets, number = input("Please enter the number corresponding to your ticket choice followed by the number of tickets you would like to purchase: ").split()
        order.append((Tickets, number))
        choice = input("Would you like to purchase more tickets?" )
        if choice != "yes":
            complete = True
    return order

order = get_ticket_order()            

order will end up being a list with multible sublists each having 2 values, 1 for the ticket, and the other for the quantity. In order to access those, you just need to extract them using indexing or iterate through them with a for loop. for example:

order = [[4,1], [2,2], [3,1]]

for ticket, quantity in order:
    #do something with ticket
    # do something with quantity

# or you can extract them individually with indexes

ticket1, quantity1 = order[0] # ticket1 now = 4 quantity1 now = 1
ticket2, quantity2 = order[1] # ticket2 now = 2 quantity2 now = 2

Upvotes: 0

belyakov-am
belyakov-am

Reputation: 21

The most straightforward way is to collect tuples (index, count), where index is ticket number (it can be ticket name, though) and count is number of tickets of this type.

Make sure to validate all inputs and just reask if something went wrong.

UPD: you probably should use while loop for gathering ticket answers

Upvotes: 1

Related Questions