# 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_answers=('yes', 'no'), 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_answers: # check if the response is the entire word if response == item: return item # check if it's the "n" letters elif response == item[:num_letters]: return item print(f"Please choose an option from {valid_answers}") def instructions(): print(make_statement("Instructions", "â„šī¸")) print(''' Instructions blah blah blah ''') 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. \n") def int_check(question, low, high): """Checks users enter an integer""" error = f"❗ Invalid input. Please enter a valid number. ❗\n" while True: to_check = input(question) if to_check == "quit": return "quit" try: # change the response to an integer that it's more than zero response = int(input(question)) return response except ValueError: print(error) def currency(x): """Formats numbers as currency ($#.##)""" return "${:.2f}".format(x) # Main routine goes here # Program main heading print(make_statement("Car Budget Buyer", "🚗")) print() # Asks users name name = not_blank("Hello! What's your name? ") # Ask user if they want to see the instructions # Display them if necessary print() want_instructions = string_check(f"Hi {name}, Would you like a tutorial/explanation on how the program works? ") if want_instructions == "yes": instructions() print() # Ask for budget print(f"\nAlright {name}, Let's find a car you can afford!") while True: budget = int_check("Enter your budget (minimum $5000): $") if budget < 5000: print() print("❗ please enter a whole number that's $5000 or more ❗\n") else: print() print(f"Your budget is set to ${budget}.\n") break # Shopping cart cart = [] #Display items cars = [ {"item": "Basic Car", "price": 5000, "quantity": 10}, {"item": "Standard Car", "price": 10000, "quantity": 5}, {"item": "Luxury Car", "price": 25000, "quantity": 2}, {"item": "Sports Car", "price": 50000, "quantity": 1}, ] # Shows list while True: affordable_cars = [car for car in cars if car["price"] <= budget and car["quantity"] > 0] if not(affordable_cars): print("You can't afford any more cars or stock is out. Time to checkout.") break print("Cars you can afford:") for i, car in enumerate(affordable_cars, start=1): print(f"{i}. {car['item']} - ${car['price']} (In stock: {car['quantity']})") print() choice = int_check("Which car would you like to purchase?, or type '0' to quit: ").lower() if choice == "0": break # Adds car selected to cart # Checkout if not cart: print("Thank you for using Car Budget Buyer!") exit() print(make_statement("🛒 Checkout Summary", "🛒")) # ---- Receipt Generation ----