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', 'pick-up', 'delivery'), num_letters=1): """Checks input is in a list or matches first 'n' letters""" while True: response = input(question).lower() for items in valid_answers: if response == items or response == items[:num_letters]: return items 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) def get_phone_number(): """Prompts for a phone number using simple logic (no .strip or .isdigit)""" while True: phone = input("Please enter your phone number: ") try: int(phone) return phone except ValueError: print("Please enter a valid number using digits only.") # pizza ordered list user_pizza_choice = [] # List to store chosen pizza names user_pizza_cost = [] # List to store corresponding pizza prices order_dict = { 'Item': user_pizza_choice, 'Price': user_pizza_cost } # Program starts make_statement("Pizza Palace", "🍕") # Display program title print() want_instructions = string_check("Do you want to see the instructions? ") # Ask user if they want instructions if want_instructions == "yes": instructions() # Display instructions if user says yes # Constants MAX_PIZZAS = 5 # Max pizzas allowed per order pizzas_sold = 0 # Counter for pizzas sold payment_ans = ('cash', 'credit') # Accepted payment types CREDIT_SURCHARGE = 0.05 # Credit incurs 5% extra charge # 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 # All regular pizzas cost $6.50 special_price = [6.75, 7] # Prices for special pizzas # Combined lists all_pizzas = pizza_types + special_pizzas # All pizza names all_prices = pizza_cost + special_price # All pizza prices # 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) # Regular pizza menu pizza_frame.index = pizza_frame.index + 1 pizza_frame['Price'] = pizza_frame['Price'].apply(currency) # Format prices special_frame = pandas.DataFrame(special_dict) # Special pizza menu special_frame.index = special_frame.index + 1 special_frame['Price'] = special_frame['Price'].apply(currency) # user order # Show menu once make_statement("Pizza Menu", "🍕") # Display menu heading print(pizza_frame) # Show regular pizzas make_statement("", "----") print(special_frame) # Show special pizzas # Get customer name and payment method once print() name = not_blank("Name (or 'xxx' to quit): ") # Ask for name if name == "xxx": # Exit condition print("program ended") exit() phone_number = get_phone_number() # Ask for phone number pay_method = string_check("Payment method (5% surcharge for credit): ", payment_ans, 2) # Get payment type if pay_method == "cash": surcharge_rate = 0 # No surcharge else: surcharge_rate = CREDIT_SURCHARGE # Apply surcharge print() print(f"{name} is ordering with {pay_method}") # Confirm payment 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) # Choose pizza type if type_of_pizza == 1: pizza_choice = int_check("Enter pizza order number (1-10)", 1, 10) # Choose from regular 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 # Increment pizza count elif type_of_pizza == 2: pizza_choice = int_check("Enter special pizza order number (1-2)", 1, 2) # Choose from special 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 # Increment pizza count if pizzas_sold < MAX_PIZZAS: like_pizza = string_check("Would you like another pizza? ") # Ask if user wants more print(f"{pizzas_sold}/{MAX_PIZZAS} Pizzas Sold") if like_pizza == "no": print("Thanks for your order!") break # Exit loop if user is done continue # user order receipt order_frame = pandas.DataFrame(order_dict) # Create DataFrame from order order_frame.index = order_frame.index + 1 order_frame['Price'] = order_frame['Price'].apply(currency) # Format prices print(order_frame) # Display receipt print() Order_cost = sum(user_pizza_cost) # Calculate order total surcharge = Order_cost * surcharge_rate # Calculate surcharge total = Order_cost + surcharge # Final total print(f" Surcharge: {currency(surcharge)}") # Show surcharge print(f" Total: {currency(total)}") # Show final amount # Ask for delivery or pickup pickup_delivery = string_check("Would you like pick-up or delivery? ", ('pick-up', 'delivery'), 1) if pickup_delivery == "pick-up": print("You have chosen pickup, Please come and pickup in 15 minutes") elif pickup_delivery == "delivery": delivery_address = not_blank("You have chosen delivery, Please enter your delivery address: ") print() print(f"{delivery_address}") # Confirm delivery address print() # order confirmation confirm = string_check("Is this your order? (yes/no)") # create file to hold data (add .txt extension) file_name = "Pizza Palace" write_to = "{}.txt".format(file_name) text_file = open(write_to, "w+") # strings to write file... heading = "=== Receipt ===\n" user_details = (f"Name: {name} \n" f"Phone Number: {phone_number} \n" f"Payment Method: {pay_method} \n") order_details = f"{order_frame}" surcharge_details = f"Surcharge: {currency(surcharge)}" total_details = f"Total: {currency(total)}" pickup_or_delivery = f"{pickup_delivery}" # list of strings... to_write = [heading, "\n", user_details, order_details, surcharge_details, total_details, pickup_or_delivery] # print the output for item in to_write: print(item) # write the item to file for item in to_write: text_file.write(item) text_file.write("\n") print() print("program ended") # End message