import pandas 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 buyer enter: -Name -Budget This program displays a range of pokemon cards users can purchase within their budget. They can choose how many cards they want, and then finally make the purchase. Users can buy multiple cards or can decide against it and then leave the code. It will finally show an itemised list of what users chose to purchase ''') 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 that users enter an integer is between 2 values""" error = f"Oops - please enter an integer 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) # Main Routine goes here # puts a cap on the max that users can spend MAX_SPEND = 3000 # lists to hold Pokémon cards all_cards = ["Blastoise (Base)", "Mewtwo (Holo)", "Gengar (Reverse)", "Charizard (Holo)", "Venusaur (Base)", "Umbreon (Gold Star)", "Espeon (Full Art)", "Lugia (Rainbow Rare)", "Rayquaza (Secret Rare)", "Pikachu (Japanese Promo)"] all_card_costs = [100, 200, 350, 425, 700, 950, 1100, 1600, 2000, 2500] # creating the dictionary pokemon_card_dict = { 'Pokemon Card': all_cards, 'Pokemon Card Price': all_card_costs, } # create dataframe / table from dictionary pokemon_card_frame = pandas.DataFrame(pokemon_card_dict) # Rearranging index pokemon_card_frame.index = numpy.arange(1, len(pokemon_card_frame) + 1) # Program main heading print(make_statement("Pokemon Card Comparison", "💲")) name = not_blank("What is your name: ") # Ask user if they want to see the instructions # display them if necessary print() want_instructions = string_check(f"Do you want to see the instructions {name}? ") if want_instructions == "yes": instructions() print() # Ask for their budget between 100 and 3000 budget = int_check("What is your budget? ", 100, 3000) purchased_cards = [] purchased_cards_prices = [] # creating dictionary for receipt purchased_dict = { 'Card Name': purchased_cards, 'Card Price ($)': purchased_cards_prices } # Loop to get name, age and payment details while budget <= MAX_SPEND: print() # Display menu items print("This is the list of cards we have on offer :") print(pokemon_card_frame) # Prompt the user to select a row using the index try: # ask for the user item choice = int_check("Enter the number of the card you would like to choose: ", 1, 10) # takes 1 off of their entry to then equate to dataframe (which starts at 0) selected_row = all_cards[choice - 1] card_price = all_card_costs[choice - 1] # if user budget is above the price of the card they can select and then take off their total budget if budget >= card_price: budget -= card_price # adds selection to their receipt purchased_cards.append(selected_row) purchased_cards_prices.append(card_price) # display user selection print(f"You selected: {selected_row} ${card_price}") print(f"your remaining budget is: ${budget} ") # if they cannot afford chosen card they will be sent to top of loop else: print("You cannot afford this card. Please choose a different one.") continue # breaks code if they cannot afford the minimum card price ($100) if budget < 100: print(f"Your remaining budget (${budget}) is too small to purchase another card." f" You will now be taken to your receipt.") break # ask if they would like to make another purchase repurchase = string_check("Would you like to make another purchase? ") if repurchase == "no": break except ValueError: print("Please enter a valid number.") # end of loop # make and display receipt print() # create dataframe / table from all selected cards purchased_cards_frame = pandas.DataFrame(purchased_dict) # rearranging to start at 1 purchased_cards_frame.index = numpy.arange(1, len(purchased_cards_frame) + 1) total_spent = sum(purchased_cards_prices) if purchased_cards: print(make_statement(f" {name}'s Purchased Cards", "🛒")) print() print(purchased_cards_frame) print() print(f"Your total amount spent is ${total_spent}. Your left over budget is ${budget}.") print() print("🎉Thank you for your purchase! Have a great day!")