Worksheet1 Python
Worksheet1 Python
Aim: Imagine you are building an e-commerce platform. Discuss how you would use
lists to manage a user's shopping cart. Explain how you could add items to the cart,
remove items, and calculate the totla cost of the items. Use Following terms:Data
structure: List operation:A. append()B. insert().
Source Code:
print("----------Welcome To Ecommerce Cart Manager-----------")
cart = []
cost = []
while True:
num = int(input("Enter Your Choice\n1. Add New Product in the List\n2. Append another item in the
cart at a particular place\n3. Search a Product in the cart\n4. Check what your cart contains\n5. Total
Cost of the items in the cart\n6. Remove an item\n"))
if num == 1:
for i in range(n):
cart.append(s)
cost.append(c)
elif num == 2:
pos = int(input("Enter the position where you want to add the product\n"))
cart.insert(pos, s)
cost.insert(pos, c)
elif num == 3:
if s2 in cart:
index = cart.index(s2)
print("Product Details:\n")
else:
elif num == 4:
for i in range(len(cart)):
2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
print()
elif num == 5:
total_cost = sum(cost)
print()
elif num == 6:
if s in cart:
index = cart.index(s)
del cart[index]
del cost[index]
else:
else:
print("Invalid Choice\n")
if a == 0:
break
3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT:
4
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING