# 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): while True: response = input(question).lower() for item in valid_answers: if response == item or 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 You will input a budget and be shown cars you can afford. You can add cars to a cart and then confirm your purchase at the end. ''') 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 or (maximum is not None and value > maximum): if maximum: print(f"❗ Please enter a whole number between {minimum} and {maximum}. ❗") print() else: print(f"❗ Please enter a whole number of at least {minimum}. ❗") print() else: return value except ValueError: if maximum: print(f"❗ Please enter a whole number between {minimum} and {maximum}. ❗") print() else: print(f"❗ Please enter a whole number of at least {minimum}. ❗") print() 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 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", "🚗")) print() name = not_blank("Hello! What's your name? ") # Ask if they want instructions 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!") budget = int_check("Enter your budget (minimum $5000): $", minimum=5000) print(f"Your budget is set to ${budget}.\n") # Shopping cart cart = [] 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("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 add to your cart (or 0 to checkout): ", minimum=0, maximum=len(affordable_cars)) if choice == 0: 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 add up to {max_can_buy} of {selected_car['name']} to your cart.") quantity = int_check(f"How many {selected_car['name']} would you like to add? ", minimum=1, maximum=max_can_buy) total_cost = quantity * selected_car['price'] cart.append({ "name": selected_car['name'], "price": selected_car['price'], "quantity": quantity, "total_cost": total_cost }) selected_car['quantity'] -= quantity budget -= total_cost print(f"{quantity} x {selected_car['name']} added to cart. Remaining budget: ${budget}\n") # Checkout if not cart: print("You didn't add anything to your cart. Goodbye!") else: print(make_statement("🛒 Checkout Summary", "🛒")) final_total = 0 for item in cart: print(f"{item['quantity']} x {item['name']} = ${item['total_cost']}") final_total += item['total_cost'] print(f"\nTotal Cost: ${final_total}") confirm = yes_no("Are you sure you want to complete the purchase? (yes/no): ") if confirm: print("✅ Purchase complete!") else: print("❌ Purchase cancelled. Your cart has been emptied.")