import pandas # Functions def make_statement(statement, decoration, lines=1): """Creates headings (3 lines), subheadings (2 lines) and emphasised text (1 line)""" middle = f"{decoration * 3} {statement} {decoration * 3}" top_bottom = decoration * len(middle) if lines == 1: print(middle) elif lines == 2: print(middle) print(top_bottom) else: print(top_bottom) print(middle) print(top_bottom) def string_check(question, valid_answers=('yes', 'no'), num_letters=1): """Checks input is in a list or matches first 'n' letters""" while True: response = input(question).lower() for item in valid_answers: if response == item or response == item[:num_letters]: return item print(f"Please choose an option from {valid_answers}") def instructions(): make_statement("Instructions", "â„šī¸") print(''' For each order enter: - Their name - Their age - The payment method (cash / credit) program will record pizza sales and calculate the order cost. Once you have either sold all 5 pizzas or entered the exit code ('xxx'), the program will display the receipt and write the data to a text file. ''') def not_blank(question): """Ensures input is not blank""" while True: response = input(question) if response != "": return response print("Sorry, this can't be blank. Please try again.\n") def int_check(question, low, high): """Ensures user enters an integer or 'xxx'""" error = "Oops - please enter an integer between the given range." while True: try: response = int(input(question)) if low <= response <= high: return response else: print(error) except ValueError: print(error) def currency(x): """Formats numbers as currency ($#,##)""" return "${:.2f}".format(x) # pizza ordered list user_pizza_choice = [] user_pizza_cost = [] order_dict = { 'Item': user_pizza_choice, 'Price': user_pizza_cost } # Program starts make_statement("Pizza Palace", "🍕") print() want_instructions = string_check("Do you want to see the instructions? ") if want_instructions == "yes": instructions() # Constants MAX_PIZZAS = 5 pizzas_sold = 0 payment_ans = ('cash', 'credit') CREDIT_SURCHARGE = 0.05 # Pizza types and prices pizza_types = ["Cheese", "Pepperoni", "Ham & Cheese", "BBQ", "Veggie", "Meat-lovers", "Spicy chicken", "Pesto", "Hawaiian", "Burger"] special_pizzas = ["Dessert Pizza", "Butter Chicken"] pizza_cost = [6.50] * 10 special_price = [6.75, 7] # Combined lists all_pizzas = pizza_types + special_pizzas all_prices = pizza_cost + special_price # Create and format menu DataFrames pizza_dict = {'Pizza': pizza_types, 'Price': pizza_cost} special_dict = {'Special Pizza': special_pizzas, 'Price': special_price} pizza_frame = pandas.DataFrame(pizza_dict) pizza_frame.index = pizza_frame.index + 1 pizza_frame['Price'] = pizza_frame['Price'].apply(currency) special_frame = pandas.DataFrame(special_dict) special_frame.index = special_frame.index + 1 special_frame['Price'] = special_frame['Price'].apply(currency) # user order # Show menu once make_statement("Pizza Menu", "🍕") print(pizza_frame) make_statement("", "----") print(special_frame) # Get customer name and payment method once print() name = not_blank("Name (or 'xxx' to quit): ") if name == "xxx": print("program ended") exit() pay_method = string_check("Payment method (5% surcharge for credit): ", payment_ans, 2) if pay_method == "cash": surcharge_rate = 0 else: surcharge_rate = CREDIT_SURCHARGE print() print(f"{name} is ordering with {pay_method}") # Order loop while pizzas_sold < MAX_PIZZAS: print() type_of_pizza = int_check("Would you like regular pizza (1) or special pizza (2)?", 1, 2) if type_of_pizza == 1: pizza_choice = int_check("Enter pizza order number (1-10)", 1, 10) print() print(f"-{pizza_types[pizza_choice - 1]}") user_pizza_choice.append(pizza_types[pizza_choice - 1]) user_pizza_cost.append(6.5) print() pizzas_sold += 1 elif type_of_pizza == 2: pizza_choice = int_check("Enter special pizza order number (1-2)", 1, 2) print() print(f"-{special_pizzas[pizza_choice - 1]}") user_pizza_choice.append(special_pizzas[pizza_choice - 1]) user_pizza_cost.append(special_price[pizza_choice - 1]) print() pizzas_sold += 1 if pizzas_sold < MAX_PIZZAS: like_pizza = string_check("Would you like another pizza? ") print(f"{pizzas_sold}/{MAX_PIZZAS} Pizzas Sold") if like_pizza == "no": print("Thanks for your order!") break continue # user order receipt order_frame = pandas.DataFrame(order_dict) order_frame.index = order_frame.index + 1 order_frame['Price'] = order_frame['Price'].apply(currency) print(order_frame) print() Order_cost = sum(user_pizza_cost) surcharge = Order_cost * surcharge_rate total_due = Order_cost + surcharge print(f"Subtotal: {currency(Order_cost)}") print(f"Surcharge: {currency(surcharge)}") print(f"Total Due: {currency(total_due)}") print("program ended")