import pandas # Import pandas for DataFrame creation and manipulation # 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 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 burger sales and calculate the order cost. Once you have either sold all 5 burgers 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) # burger ordered list user_burger_choice = [] # List to store chosen burger names user_burger_cost = [] # List to store corresponding burger prices order_dict = { 'Item': user_burger_choice, 'Price': user_burger_cost } # Program starts make_statement("Burger Barn", "🍔") # 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_BURGERS = 5 # Max burgers allowed per order burgers_sold = 0 # Counter for burgers sold payment_ans = ('cash', 'credit') # Accepted payment types CREDIT_SURCHARGE = 0.05 # Credit incurs 5% extra charge # Burger types and prices burger_types = ["Cheese Burger", "Bacon Burger", "Chicken Burger", "BBQ Burger", "Veggie Burger", "Double Beef", "Spicy Chicken Burger", "Fish Burger", "Hawaiian Burger", "Classic Burger"] special_burgers = ["Loaded Burger", "Butter Chicken Burger"] burger_cost = [6.50] * 10 # All regular burgers cost $6.50 special_price = [6.75, 7] # Prices for special burgers # Combined lists all_burgers = burger_types + special_burgers # All burger names all_prices = burger_cost + special_price # All burger prices # Create and format menu DataFrames burger_dict = {'Burger': burger_types, 'Price': burger_cost} special_dict = {'Special Burger': special_burgers, 'Price': special_price} burger_frame = pandas.DataFrame(burger_dict) # Regular burger menu burger_frame.index = burger_frame.index + 1 burger_frame['Price'] = burger_frame['Price'].apply(currency) # Format prices special_frame = pandas.DataFrame(special_dict) # Special burger menu special_frame.index = special_frame.index + 1 special_frame['Price'] = special_frame['Price'].apply(currency) # user order # Show menu once make_statement("Burger Menu", "🍔") # Display menu heading print(burger_frame) # Show regular burgers make_statement("", "----") print(special_frame) # Show special burgers # 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() 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 burgers_sold < MAX_BURGERS: print() type_of_burger = int_check("Would you like regular burger (1) or special burger (2)?", 1, 2) # Choose burger type if type_of_burger == 1: burger_choice = int_check("Enter burger order number (1-10)", 1, 10) # Choose from regular print() print(f"-{burger_types[burger_choice - 1]}") user_burger_choice.append(burger_types[burger_choice - 1]) user_burger_cost.append(6.5) print() burgers_sold += 1 # Increment burger count elif type_of_burger == 2: burger_choice = int_check("Enter special burger order number (1-2)", 1, 2) # Choose from special print() print(f"-{special_burgers[burger_choice - 1]}") user_burger_choice.append(special_burgers[burger_choice - 1]) user_burger_cost.append(special_price[burger_choice - 1]) print() burgers_sold += 1 # Increment burger count if burgers_sold < MAX_BURGERS: like_burger = string_check("Would you like another burger? ") # Ask if user wants more print(f"{burgers_sold}/{MAX_BURGERS} Burgers Sold") if like_burger == "no": print("Thanks for your order!") break # Exit loop if user is done continue # Go to next iteration # 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_burger_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 pick up 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() # create file to hold data (add .txt extension) file_name = "Burger Borough" 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"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