#Function goes here def make_statement(statement, decoration): return f"{decoration * 3} {statement} {decoration * 3}" # Checks for Yes / No input 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}") # Displays Instructions def instructions(): print(make_statement("Instructions", "â„šī¸")) print(''' Instructions blah blah blah ''') def not_blank(question): while True: response = input(question).strip() if response: return response print("Sorry, this cannot be blank. Please try again.") def int_check(question, minimum=1, maximum=None): while True: try: value = int(input(question)) if value < minimum: print(f"Please enter a number greater than or equal to {minimum}.") elif maximum is not None and value > maximum: print(f"Please enter a number less than or equal to {maximum}.") else: return value except ValueError: print("Please enter a valid integer.") def yes_no(question): while True: response = input(question).strip().lower() if response in ('yes', 'y'): return True elif response in ('no', 'n'): return False else: print("Please answer 'yes' or 'no'.") # Define cars with price and quantity (more stock for cheaper cars) cars = [ {"name": "Basic Car", "price": 5000, "quantity": 10}, {"name": "Standard Car", "price": 10000, "quantity": 5}, {"name": "Luxury Car", "price": 25000, "quantity": 2}, {"name": "Sports Car", "price": 50000, "quantity": 1}, ] print(make_statement("Car Budget Buyer", "🚗")) 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, must be at least 5000 print(f"\nAlright {name}, Let's find a car you can afford!") budget = int_check("Enter your budget (minimum $5000): $", minimum=5000) print(f"Your budget is set to ${budget}.\n") # Filter cars that user can afford at least one of affordable_cars = [car for car in cars if car["price"] <= budget] if not affordable_cars: print("Sorry, no cars available within your budget.") exit() while True: print("Here are the cars you can afford:") for i, car in enumerate(affordable_cars, 1): print(f"{i}. {car['name']} - ${car['price']} (Stock: {car['quantity']})") choice = int_check(f"\nEnter the number of the car you'd like to buy (or 0 to exit): ", minimum=0, maximum=len(affordable_cars)) if choice == 0: print("Thanks for visiting! Goodbye.") break selected_car = affordable_cars[choice - 1] max_can_buy_stock = selected_car['quantity'] max_can_buy_budget = budget // selected_car['price'] max_can_buy = min(max_can_buy_stock, max_can_buy_budget) if max_can_buy == 0: print(f"Sorry, you cannot afford any {selected_car['name']} with your current budget.") continue print(f"\nYou can buy up to {max_can_buy} of {selected_car['name']}.") quantity = int_check(f"How many {selected_car['name']} would you like to buy? ", minimum=1, maximum=max_can_buy) total_cost = quantity * selected_car['price'] print(f"\nYou are about to purchase {quantity} x {selected_car['name']} for ${total_cost}.") if yes_no("Are you sure? (yes/no): "): budget -= total_cost selected_car['quantity'] -= quantity print(f"Purchase successful! Your remaining budget is ${budget}.\n") # Update affordable cars after purchase 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. Thank you for shopping!") break else: print("Purchase cancelled.\n")