import pandas as pd # Fix: Proper pandas import import random def make_statement(statement, decoration): print(f"{decoration * 3} {statement} {decoration * 3}") def string_check(question, valid_ans_list=('yes', 'no'), num_letters=1): while True: response = input(question).lower() for item in valid_ans_list: if response == item: return item elif response == item[:num_letters]: return item print(f"Please choose an option from {valid_ans_list}") def instructions(): make_statement(statement="Instructions", decoration="â„šī¸") print(''' For each ticket holder enter ... - Their name - Their age - The payment method (cash / credit) The program will record the ticket sale and calculate the ticket cost (and the profit). Once you have either sold all of the tickets or entered the exit code ('xxx'), the program will display the ticket sales information and write the data to a text file. It will also choose one lucky ticket holder who wins the draw (their ticket is free). ''') def not_blank(question): while True: response = input(question).strip() if response != "": return response print("Sorry, this can't be blank. Please try again.\n") def int_check(question): error = "Oops - please enter an integer." while True: try: response = int(input(question)) return response except ValueError: print(error) def currency(x): return "${:.2f}".format(x) # MAIN PROGRAM STARTS HERE Max_ticket = 5 tickets_sold = 0 payment_ans = ('cash', 'credit') c_price = 7.50 a_price = 10.50 s_price = 6.50 credit_sur = 0.05 base_cost = 5.00 # --------- Lists to Hold Ticket Info --------- all_names = [] all_ticket_costs = [] all_surcharges = [] make_statement(statement="Mino Movie Fundraiser Program", decoration="đŸŋ") print() want_intro = string_check("Do you want to see the instructions? ") if want_intro == "yes": instructions() print() while tickets_sold < Max_ticket: print() name = not_blank("Name: ") if name == "xxx": break age = int_check("Age: ") if age < 12: print(f"{name} is too young.") continue elif age < 16: ticket_price = c_price elif age < 65: ticket_price = a_price elif age <= 120: ticket_price = s_price else: print(f"{name} is too old.") continue pay_method = string_check("Payment method (cash/credit): ", payment_ans, num_letters=2) if pay_method == "cash": surcharge = 0 else: surcharge = ticket_price * credit_sur # Append to lists all_names.append(name) all_ticket_costs.append(ticket_price) all_surcharges.append(surcharge) tickets_sold += 1 # Create DataFrame after data is collected mini_movie_dict = { 'Name': all_names, 'Ticket Price': all_ticket_costs, 'Surcharge': all_surcharges } mini_movie_frame = pd.DataFrame(mini_movie_dict) # Add Total and Profit columns mini_movie_frame['Total'] = mini_movie_frame['Ticket Price'] + mini_movie_frame['Surcharge'] mini_movie_frame['Profit'] = mini_movie_frame['Ticket Price'] - base_cost # Work out total paid and total profit total_paid = mini_movie_frame['Total'].sum() total_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) # Output movie frame without index print("\n----- Ticket Summary -----") print(mini_movie_frame.to_string(index=False)) # Print totals print() print(f"Total Paid: ${total_paid:.2f}") print(f"Total Profit: ${total_profit:.2f}") winner = random.choice(all_names) # Get winner's index winner_index = all_names.index(winner) print("Winner:", winner, "List position:", winner_index) # Get their total ticket value using DataFrame total_won = mini_movie_frame.at[winner_index, 'Total'] # Final winner announcement print() print(f"🎉 The lucky winner is {winner}. Their ticket worth {total_won} is free! 🎉") if tickets_sold == Max_ticket: print(f"You have sold all the tickets (ie: {Max_ticket} tickets)") else: print(f"You have sold {tickets_sold} / {Max_ticket} tickets.")