Robotics & Artificial Intelligence
Write a program to implement a simple shopping cart system. In it user can add items, remove items, view the cart, and calculate the total cost.
Answer
cart = []
price = []
choice = 0
while choice != 5:
print("1. Add item")
print("2. Remove item")
print("3. View cart")
print("4. Calculate total cost")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
item = input("Enter item name: ")
cost = int(input("Enter item price: "))
cart.append(item)
price.append(cost)
print("Item added to cart")
elif choice == 2:
item = input("Enter item name to remove: ")
if item in cart:
index = cart.index(item)
cart.pop(index)
price.pop(index)
print("Item removed from cart")
else:
print("Item not found")
elif choice == 3:
print("Items in cart:")
for i in range(len(cart)):
print(cart[i], "-", price[i])
elif choice == 4:
total = 0
for p in price:
total = total + p
print("Total cost:", total)
elif choice == 5:
print("Thank you for shopping")
else:
print("Invalid choice")Output
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 1
Enter item name: Pen
Enter item price: 50
Item added to cart
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 1
Enter item name: Book
Enter item price: 100
Item added to cart
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 1
Enter item name: Pencil
Enter item price: 15
Item added to cart
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 2
Enter item name to remove: Pen
Item removed from cart
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 3
Items in cart:
Book - 100
Pencil - 15
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 4
Total cost: 115
1. Add item
2. Remove item
3. View cart
4. Calculate total cost
5. Exit
Enter your choice: 5
Thank you for shopping
Related Questions
Write a python program to print a pyramid using 'for' loop.
Write a program to store marks of your class students and show their percentage.
Write a program that inputs two tuples and creates a third one that contains all the elements of the first tuple followed by all the elements of the second tuple.
Write a program to print the longest word in the given list of words.