import pandas from tabulate import tabulate # from datetime import date import math # Importing numpy package import numpy as np # Functions go here def make_statement(statement, decoration): """Emphasis headings by adding decoration at the start and end""" return f"{decoration * 3} {statement} {decoration * 3}" def yes_no_check(question): while True: response = input(question).lower() # checks user response, question repeats if users don't enter y/n if response == "yes" or response == "y": return "yes" elif response == "no" or response == "n": return "no" else: print("Please enter yes / no\n") def instructions(): make_statement("Instructions", "❕") print(''' For each burger wanted type... - The burgers name - How many burgers - The size - Extra toppings The program will record the amount of burgers, the types of burgers, the size of burgers, the extra toppings, and the cost. Once you have either bought 5 burgers or wish to finish your order the program will display the information on your burger and write the data to a text file. Enter corresponding number for items on menu to purchase. ''') def string_check(question, valid_answer, num_letters=1): """Checks that users enter the full word or the 'n' letter/s of a word from a list of valid responses""" while True: response = input(question).lower() for item in valid_answer: # check if the response is the entire word if response == item: return item # check if it's the first letter elif response == item[:num_letters]: return item print(f"Please choose an option from {valid_answer}") def menu(): make_statement("Menu", "📃") print(''' Burgers: 1.Chicken 2.Bacon 3.Cheese 4.Bacon and Egg 5.Vegan Extra Toppings: 1. Cheese 2. Ham 3. Pineapple 4. Bacon 5. Onion All Extra Topping $1 ''') def not_blank(question): """Checks that a user response is not blank""" while True: response = input(question) if response != "": return response print("Sorry, this cant be blank. Please try again.\n") def num_check(question, low, high): """Checks users enter an integer / float that us higher than zero (or the optional exit code)""" while True: error = f"Enter a number between {low} and {high}" # check datatype is correct and that number is more than zero try: response = int(input(question)) # check for valid number if low <= response <= high: return response else: print(error) except ValueError: print(error) def get_expenses(exp_type, how_many=1): """Gets variable / fixed expenses and outputs panda (as a string) and a subtotal of the expenses""" # Lists for panda all_items = [] all_amounts = [] all_dollar_per_item = [] # Expenses dictionary expenses_dict = { "Item": all_items, "Amount": all_amounts, "$ / Item": all_dollar_per_item } # defaults for fixed expenses amount = how_many # how_many defaults to 1 # loop to get expenses while True: # Get item name and check it's not blank item_name = not_blank("Item Name: ") # check users enter at least one variable expense if exp_type == "variable" and item_name == "xxx" and len(all_items) == 0: print("Oops - you have not entered anything. " "You need at least 1 items.") continue # end loop when users enter exit code elif item_name == "xxx": break # Get variable expenses item amount defaults to number of products being made. if exp_type == "variable": amount = num_check(f"How many ", "integer", "") # Allows users to pus to default to number of items being made if amount == "": amount = how_many how_much_question = "Price for one? $" # Get price for item (question customised depending on expense type). price_for_one = num_check(how_much_question, "float", 0) all_items.append(item_name) all_amounts.append(amount) all_dollar_per_item.append(price_for_one) # make panda expense_frame = pandas.DataFrame(expenses_dict) # Calculate Row Cost expense_frame['Cost'] = expense_frame['Amount'] * expense_frame['$ / Item'] # calculate subtotal subtotal = expense_frame['Cost'].sum() # Apply currency formatting to currency columns. add_dollars = ['Amount', '$ / Item', 'Cost'] for var_item in add_dollars: expense_frame[var_item] = expense_frame[var_item].apply(currency) # make expense frame into a string with the desired columns if exp_type == "variable": expense_string = tabulate(expense_frame, headers='keys', tablefmt='psql', showindex=False) else: expense_string = tabulate(expense_frame[['Item', 'Cost']], headers='keys', tablefmt='psql', showindex=False) # return all items for now so we can check loop. return expense_string, subtotal def currency(x): """Formats numbers as currency ($#.##)""" return "${:.2f}".format(x) def round_up(amount, round_val): """Rounds amount to desired hole number""" return int(math.ceil(amount / round_val)) * round_val def profit_goal(total_costs): """Calculates profit goal work out profit goal and total sales required""" # Initialise variables and error message error = "Please enter a valid profit goal\n" valid = False while not valid: # ask for profit goal... response = input("What is your profit goal (eg $500 or 50%): ") # check if first character is $ if response[0] == "$": profit_type = "$" # Get amount (everything after the $) amount = response[1:] # check if last character is % elif response[-1] == "%": profit_type = "%" # Get amount (everything before the %) amount = response[:-1] else: # set response to amount for now profit_type = "unknown" amount = response try: # Check amount is a number more than zero... amount = float(amount) if amount <= 0: print(error) continue except ValueError: print(error) continue if profit_type == "unknown" and amount >= 100: dollar_type = yes_no_check(f"Do you mean ${amount:.2f}. ie {amount:.2f} dollars?") # Set profit type based on user answer above if dollar_type == "yes": profit_type = "%" else: profit_type = "%" elif profit_type == "unknown" and amount < 100: percent_type = yes_no_check(f"Do you mean {amount}%? , y / n: ") if percent_type == "yes": profit_type = "%" else: profit_type = "$" # return profit goal to main routine if profit_type == "$": return amount else: goal = (amount / 100) * total_costs return goal # Main routine goes here # Initialise variables... MAX_BURGERS = 5 MAX_PER_ORDER = 3 total_burger_made = 0 total_cost_burger = 0 size_options = ["large", "medium", "small"] burger_list = [ "Chicken", "Bacon", "Cheese", "Bacon and Egg", "Vegan" ] toppings_list = [ "Cheese", "Ham", "Pineapple", "Bacon", "Pickles" ] burger_size_price = [5, 7.50, 10] extra_topping_price = [1] cust_burger = [] cust_burger_size = [] cust_burger_cost = [] cust_extra_toppings = [] print(make_statement("Isaacs Burger Shop", "🍕")) print() want_instructions = yes_no_check("Do you want to see the instructions? ") if want_instructions == "yes": instructions() print() want_menu = yes_no_check("Do you want to see the menu? ") if want_menu == "yes": menu() print() while True: max_remaining = MAX_BURGERS - total_burger_made if max_remaining == 0: print("you have reached maximum burgers allowed") break max_allowed = min(MAX_PER_ORDER, max_remaining) # Get product details burger_type = num_check("Which burger? ", 1, 5) quantity_made = num_check(f"Quantity being made (max {max_allowed}): ", 1, max_allowed) total_burger_made += quantity_made if total_burger_made >= MAX_BURGERS: print("max burgers brought") break print(f"You have purchased {quantity_made} {burger_list[burger_type - 1]} burgers!") # Add burger to customer list cust_burger.append(burger_list[burger_type - 1]) print("Sizes and Prices for burgers: Large ($10) Medium ($7.50) Small ($5)") burger_size = string_check("Enter the size:", size_options) if burger_size == "large" or burger_size == "l": burger_size = "large" burger_price = 10 elif burger_size == "medium" or burger_size == "m": burger_size = "medium" burger_price = 7.50 else: burger_price = 5 burger_size = "small" extra_toppings = yes_no_check("Would you like extra toppings on your burger?") if extra_toppings == "yes": ask_extra_toppings = num_check("Which extra toppings? ", 1, 5) print(f"You have purchased {toppings_list[ask_extra_toppings - 1]}!") # Add $1 to the burger price burger_price += extra_topping_price[0] # Add extra toppings to customer list cust_extra_toppings.append(toppings_list[ask_extra_toppings - 1]) elif extra_toppings == "no": print("No extra toppings will be added to your burger.") cust_extra_toppings.append("None") else: print("Invalid, Please answer with yes or no.") # Calculate total cost for this burger order single_burger_cost = burger_price * quantity_made total_cost_burger += single_burger_cost cust_order_dict = { 'Burger': cust_burger, 'Burger Amount': quantity_made, 'Burger Size': cust_burger_size, 'Extra Toppings': cust_extra_toppings, 'Cost': cust_burger_cost } cust_burger_size.append(burger_size) cust_burger_cost.append(single_burger_cost) print(f"You have spent a total of ${total_cost_burger:.2f}") # create dataframe / table from dictionary cust_order_frame = pandas.DataFrame(cust_order_dict) # Rearranging index cust_order_frame.index = np.arange(1, len(cust_order_frame) + 1) print(cust_order_frame) print(f"You currently have {total_burger_made} burgers, you can buy {MAX_BURGERS - total_burger_made} more burgers!") # ask for another burger print() another_burger = yes_no_check("Would you lke another flavour of burger? ") if another_burger == "yes": continue elif another_burger == "no": break