# 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}. ❗\n") else: print(f"❗ Please enter a whole number of at least {minimum}. ❗\n") else: return value except ValueError: print("❗ Invalid input. Please enter a valid number. ❗\n") # Currency formatting function def currency(x): return f"${x:,.2f}" # Define cars with price and quantity 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}, ] 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['item']} - ${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['item']} with your current budget.") continue print(f"\nYou can add up to {max_can_buy} of {selected_car['item']} to your cart.") quantity = int_check(f"How many {selected_car['item']} would you like to add? ", minimum=1, maximum=max_can_buy) total_cost = quantity * selected_car['price'] cart.append({ "item": selected_car['item'], "price": selected_car['price'], "quantity": quantity, "total_cost": total_cost }) selected_car['quantity'] -= quantity budget -= total_cost print(f"{quantity} x {selected_car['item']} added to cart. Remaining budget: ${budget}\n") # Checkout if not cart: print("You didn't add anything to your cart. Goodbye!") exit() print(make_statement("🛒 Checkout Summary", "🛒")) final_total = 0 for item in cart: print(f"{item['quantity']} x {item['item']} = ${item['total_cost']}") final_total += item['total_cost'] print(f"\nTotal Cost: ${final_total}") confirm = string_check("Are you sure you want to complete the purchase? (yes/no): ") print() if not confirm: print("❌ Purchase cancelled. Your cart has been emptied.") exit() print("✅ Purchase complete!") # ---- Receipt Generation ---- # Create DataFrame from cart receipt_df = pandas.DataFrame(cart) # Calculate totals per row receipt_df["Total"] = receipt_df["price"] * receipt_df["quantity"] # Format currency columns for column in ["price", "Total"]: receipt_df[column] = receipt_df[column].apply(currency) # Create receipt layout receipt_string = receipt_df.to_string(index=False) # Total calculations total_cost = sum(item["price"] * item["quantity"] for item in cart) # Format sections heading = make_statement("Car Purchase Receipt", "=") summary_heading = make_statement("Summary", "-") summary = [ f"Total Cost: {currency(total_cost)}" ] # Final output list receipt_output = [ heading, "", receipt_string, "", summary_heading, *summary ] # Display on screen print("\n" + "\n".join(receipt_output)) # Save to file filename = "car_purchase_receipt.txt" with open(filename, "w") as file: for line in receipt_output: file.write(line + "\n")