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, the best value for their budget and then finally purchase. Users can buy multiple cards or can enter (xxx) to 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)", "Gen gar (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] 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) selected_row = all_cards[choice - 1] card_price = all_card_costs[choice - 1] if budget >= card_price: budget -= card_price 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 new budget is: ${budget} ") else: print("You cannot afford this card. Please choose a different one.") continue repurchase = string_check("Would you like to make another purchase? ") if repurchase == "no": break else: print("Invalid number. Please choose between 1-10.") continue 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}")