import pandas import random # functions go here # print instructions when function called def show_instructions(): print('''\n *** Instructions *** For each ticket, enter: - The person's name (cannot be blank) - The age (must be between 12 and 120) - Payment method (cash or credit) When you have entered all users, type 'xxx' to quit. The program will then display the ticket details, including the cost of each ticket, the total cost, and the total profit. This info will also be automatically written to a text file. ''') # check that something is a number def num_check(question): while True: try: response = int(input(question)) return response except ValueError: print("Please enter an integer.") # calculate ticket price based on age def calc_ticket_price(var_age): # ticket is $7.50 for users under 16 if var_age < 16: price = 7.5 # ticket is $10.50 for users inbetween 16 and 64 elif var_age < 65: price = 10.5 # ticket is $6.50 for seniors 65+ else: price = 6.5 return price # check that users enter a valid response # (eg yes / no / cash / credit based on a list of options) def string_checker(question, num_letters, valid_responses): error = "Please choose {} or {}".format(valid_responses[0], valid_responses[1]) if num_letters == 1: short_version = 1 else: short_version = 2 while True: response = input(question).lower() for item in valid_responses: if response == item[:short_version] or response == item: return item print(error) # currency formatting function def currency(x): return "${:.2f}".format(x) # main routine starts here # set maximum number of tickets below MAX_TICKETS = 5 tickets_sold = 0 yes_no_list = ["yes", "no"] payment_list = ["cash", "credit"] # dictionaries to hold ticket details all_names = [] all_ticket_costs = [] all_surcharge = [] mini_movie_dict = { "Name": all_names, "Ticket Price": all_ticket_costs, "Surcharge": all_surcharge } # instructions want_instructions = string_checker("Do you want to read the instructions (y/n): ", 1, yes_no_list) print("You chose", want_instructions) if want_instructions == "yes": show_instructions() # loop to sell tickets tickets_sold = 0 while tickets_sold < MAX_TICKETS: name = input("Please enter your name or 'xxx' to quit: ") if name == 'xxx' and len(all_names) > 0: break elif name == 'xxx': print("You must sell at least ONE ticket before quitting") continue age = num_check("Age: ") if 12 <= age <= 120: pass elif age < 12: print("Sorry you are too young for this movie") continue else: print("?? That looks like a typo, please try again.") continue # calculate ticket cost ticket_cost = calc_ticket_price(age) print("Age: {}, Ticket price: ${:.2f}".format(age, ticket_cost)) # get payment method pay_method = string_checker("Choose a payment method (cash / credit): ", 2, payment_list) print("You chose", pay_method) if pay_method == "cash": surcharge = 0 else: # calculate 5% surcharge if they are paying by card surcharge = ticket_cost * 0.05 tickets_sold += 1 all_names.append(name) all_ticket_costs.append(ticket_cost) all_surcharge.append(surcharge) mini_movie_frame = pandas.DataFrame(mini_movie_dict) # mini_movie_frame = mini_movie_frame.set_index('Name') # Calculate the total ticket cost (ticket + surcharge) mini_movie_frame['Total'] = mini_movie_frame['Surcharge'] \ + mini_movie_frame['Ticket Price'] # Calculate profit per ticket mini_movie_frame['Profit'] = mini_movie_frame['Ticket Price'] - 5 # Calculate totals total = mini_movie_frame['Total'].sum() profit = mini_movie_frame['Profit'].sum() # currency formatting add_dollars = ['Ticket Price', 'Surcharge', 'Total', 'Profit'] for var_item in add_dollars: mini_movie_frame[var_item] = mini_movie_frame[var_item].apply(currency) # choose winner winner_name = random.choice(all_names) # get position of winner name win_index = all_names.index(winner_name) # look up ticket price total_won = mini_movie_frame.at[win_index, 'Total'] # set index at end mini_movie_frame = mini_movie_frame.set_index('Name') print("Ticket Data") print() # output table print(mini_movie_frame) print() print("Ticket Cost") # output ticket sales and profit print("Total Ticket Sales: ${:.2f}".format(total)) print("Total Profit: ${:.2f}".format(profit)) print() print('---- Raffle Winner ----') print("Congratulations, {}. You have won {} ie: your ticket is free!".format(winner_name, total_won)) # Output number of tickets sold if tickets_sold == MAX_TICKETS: print("All tickets sold") else: print("You have sold {} ticket/s. There is {} ticket/s " "remaining".format(tickets_sold, MAX_TICKETS - tickets_sold))