import pandas as pd # Fix: Proper pandas import import random def make_statement(statement, decoration): return 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 -----") mini_movie_string = mini_movie_frame.to_string(index=False) # Print totals total_paid_string = f"Total Paid: ${total_paid:.2f}" total_profit_string = f"Total Profit: ${total_profit:.2f}" winner = random.choice(all_names) # Get winner's index winner_index = all_names.index(winner) # Get winner values and convert from $ strings to floats total_won_str = mini_movie_frame.at[winner_index, 'Total'] profit_won_str = mini_movie_frame.at[winner_index, 'Profit'] total_won = float(total_won_str.replace("$", "")) profit_won = float(profit_won_str.replace("$", "")) # Final winner announcement lucky_winner_string = f"🎉 The lucky winner is {winner}. Their ticket worth {total_won} is free! 🎉" final_total_paid_string = f"Total Paid is now ${total_paid - total_won:.2f}" final_profit_string = f"Total Profit is now ${total_profit - total_won:.2f}" if tickets_sold == Max_ticket: num_sold_string = make_statement(statement=f"You have sold all the tickets " f"(ie: {Max_ticket} tickets)", decoration="-") else: num_sold_string = make_statement(statement=f"You have sold {tickets_sold} / " f"{Max_ticket} tickets.", decoration="-") # Additional strings / Headings heading_string = make_statement(statement="Mini Movie Fundraiser", decoration="-") ticket_details_heading = make_statement(statement="Ticket Details", decoration="-") raffle_heading = make_statement(statement="--- Raffle Winner ---", decoration="-") adjusted_sales_heading = make_statement(statement="Adjusted Sales & Profit", decoration="-") adjusted_explanation = (f"We have given away a ticket worth ${total_won:.2f} which \n" f"means our sales have decreased by ${total_won:.2f} and our \n" f"profit decreased by ${profit_won:.2f}") to_write = [heading_string, "\n", ticket_details_heading, mini_movie_string, "\n", total_paid_string, total_profit_string, "\n", raffle_heading, lucky_winner_string, "\n", adjusted_sales_heading, adjusted_explanation, "\n", final_total_paid_string, final_profit_string, "\n", num_sold_string] print() for item in to_write: print(item) file_name = "MMF_ticket_details" write_to = "{}.txt".format(file_name) text_file = open(write_to, "w+")