# Importing pandas package import pandas # Importing numpy package import numpy # Functions go here def make_statement(statement, decoration): """Emphasises headings by adding decoration at the start and end""" return f"{decoration * 3} {statement} {decoration * 3}" def string_check(question, valid_ans_list=('yes', 'no'), num_letters=1): """Checks that users enter the full word or the first letter of a word from a list of valid responses""" while True: response = input(question).lower() for item in valid_ans_list: # check if the response is the entire world 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_ans_list}") def instructions(): make_statement("Instructions", "ℹ️") print(''' For each product, enter: - The product name - The total price - The quantity (e.g. 10 units) Then enter the amount of money you have to spend. The program will calculate the unit price for each item, recommend the best value item within your budget, and allow you to select how many to buy. You can continue selecting other items using your remaining budget or type the exit code ('xxx') to finish. When you exit, the program will display an itemized summary of your selections, including total cost. ''') def not_blank(question): """Checks that a user response is not blank""" while True: response = input(question) if response != "": return response print("Sorry, this can't be blank. Please try again.") def int_check(question, low, high): """Checks users enter an integer / float that is more than zero (or the 'xxx' exit code)""" error = f"Oops - please enter number between {low} and {high}." while True: try: # Change the response to an integer and check that it's more than zero 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) # Variables # lists to hold sneaker details all_sneakers = ["Nike Air Force 1", "New Balance 550", "Nike Air Max 97", "Adidas Ultraboost", "Nike Air Jordan 1", "Nike Dunk Low", "Adidas Yeezy Boost 350 V2", "Jordan 4", "Off-White x Nike Air Presto", "Balenciaga Triple S"] sneaker_price = [120, 160, 190, 220, 300, 385, 440, 685, 850, 1000] # all_surcharges = [0, 0, 0.53, 0.53, 0] sneaker_dict = { 'Sneakers': all_sneakers, 'Sneaker Price': sneaker_price, } # create dataframe / table from dictionary sneaker_frame = pandas.DataFrame(sneaker_dict) # Rearranging index sneaker_frame.index = numpy.arange(1, len(sneaker_frame) + 1) # Initialise sneaker numbers MAX_BUDGET = 1500 MAX_ITEMS = 5 sneakers_sold = 0 keep_shopping = True total_sneaker_prices = 4350 # lists to hold user purchases purchased_sneakers = [] purchase_prices = [] # Main Routine goes here # Program main heading print(make_statement("Sneaker Price Comparison Program", "💸")) # Ask user if they want to see the instructions # display them if necessary print() want_instructions = string_check("Do you want to see the instructions? ") if want_instructions == "yes": instructions() print() name = not_blank("What is your name? ") # Ask for their budget and check it's between 120 and 1500 budget = int_check("What is your budget? ", 120, 1500) # Ask user for their name (and check it's not blank) print() # Loop to get name, age and payment details while budget >= 0 and sneakers_sold < MAX_ITEMS and keep_shopping: print() # Display menu items print("This is the list of sneakers we have on offer :") print(sneaker_frame) # Prompt the user to select a row using the index choice = int_check("Enter the number of the sneaker you would like to choose: ", 1, 10) if choice in sneaker_frame.index: selected_row = all_sneakers[choice - 1] selected_sneaker_price = sneaker_price[choice - 1] # Check if the sneaker is within the user's budget if selected_sneaker_price > budget: print(f"Sorry, {selected_row} costs ${selected_sneaker_price}, which is over your budget of ${budget}") print("Please try again") continue total_sneaker_prices -= selected_sneaker_price print(f"You have selected: {selected_row} for ${selected_sneaker_price}") print(f"Price: ${selected_sneaker_price}") # Deduct price from budget budget -= selected_sneaker_price sneakers_sold += 1 print(f"Purchase confirmed! ") print(f"Remaining budget: ${budget} ") purchased_sneakers.append(selected_row) purchase_prices.append(selected_sneaker_price) # Asks User if They Want to Make an Additional Purchase additional_purchase = string_check("Would you like to purchase any more sneakers? ") if additional_purchase == "no": print("Thanks for shopping with us") keep_shopping = False break # create purchased items dataframe purchased_sneaker_dict = { 'Sneakers': purchased_sneakers, 'Sneaker Price': purchase_prices } purchased_sneaker_frame = pandas.DataFrame(sneaker_dict) # Rearranging index sneaker_frame.index = numpy.arange(1, len(sneaker_frame) + 1) # Output movie frame without index # mini_movie_string = sneaker_frame.to_string(index=False) # total_paid_string = f"Total Paid: ${total_paid:.2f}" # total_profit_string = f"Total Profit: ${total_profit:.2f}" # if sneakers_sold == MAX_BUDGET: # num_sold_string = make_statement(f"You have sold all the tickets " # f"(ie: {MAX_SNEAKERS} tickets)", "-") # else: # num_sold_string = make_statement(f"You have sold {tickets_sold} / " # f"{MAX_TICKETS} tickets.", "-") # Additional strings / Headings # heading_string = make_statement("Mini Movie Fundraiser", "=") # ticket_details_heading = make_statement("Ticket Details", "-") # raffle_heading = make_statement("--- Raffle Winner ---", "-") # adjusted_sales_heading = make_statement("Adjusted Sales & Profit", # "-") # List of strings to be outputted / written to file # to_write = [heading_string, "\n", # ticket_details_heading, # mini_movie_string, "\n", # total_paid_string, # total_profit_string, "\n", # raffle_heading, # lucky_winner_string, "\n", # adjusted_sales_heading, # adjusted_explanation, "\n", # final_total_paid_string, # final_profit_string, "\n", # num_sold_string] # Print Area # print() # for item in to_write: # print(item) # create file to hold data (add .txt extension) # file_name = "MMF_ticket_details" # write_to = f"{file_name}.txt" # text_file = open(write_to, "w+") # write the item to file # for item in to_write: # text_file.write(item) # text_file.write("\n")