shopping cart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. products ={"laptop": 75000,
  2. "smartphone": 30000,
  3. "headphones": 5000,
  4. "keyboard": 3500,
  5. "mouse": 2000
  6. }
  7. cart = []
  8. def view_products():
  9. for product, price in products.items():
  10. print(f"{product}: ${price}")
  11. def add_to_cart(product_name):
  12. if product_name in products:
  13. cart.append(product_name)
  14. print(f"{product_name}(s) added to your cart.")
  15. else:
  16. print("Product not found.")
  17. def view_cart():
  18. print("Your cart contains:")
  19. for product in cart:
  20. print(f"- {product}: ${products[product]}")
  21. def total_cart_value():
  22. total = sum(products[product] for product in cart)
  23. print(f"Total value of your cart: ${total}")
  24. def exit_cart():
  25. print("Exiting the shopping cart. Thank you for shopping with us!")
  26. while True:
  27. print("\nWelcome to the Shopping Cart!")
  28. print("1. View products")
  29. print("2. Add product to cart")
  30. print("3. View cart")
  31. print("4. Exit")
  32. user_choice = input("Enter your choice (1-4): ")
  33. if user_choice == "1":
  34. view_products()
  35. elif user_choice == "2":
  36. product_name = input("Enter the product name to add to cart: ")
  37. add_to_cart(product_name)
  38. elif user_choice == "3":
  39. view_cart()
  40. elif user_choice == "4":
  41. exit_cart()
  42. break
  43. else:
  44. print("Invalid choice. Please enter a number from 1 to 4. ")