import pandas # --------- Helper Functions --------- def int_check(question): error = "Oops - please enter an integer." while True: try: response = int(input(question)) return response except ValueError: print(error) 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 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 currency(x): return "${:.2f}".format(x) # --------- Constants --------- 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 = [] # --------- Main Ticket Loop --------- while True: print() name = not_blank("Name (or 'xxx' to stop): ") if name.lower() == "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) # --------- DataFrame and Summary --------- mini_movie_dict = { 'Name': all_names, 'Ticket Price': all_ticket_costs, 'Surcharge': all_surcharges } mini_movie_frame = pandas.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}")