-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingCart.py
84 lines (67 loc) · 2.08 KB
/
shoppingCart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#import necessary functions
from xml.etree.ElementInclude import DEFAULT_MAX_INCLUSION_DEPTH
from IPython.display import clear_output
import csv
#global list variable
cart = []
#create a function to add items
def saveCart(cart):
with open("cart.csv", mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(cart)
def add(item):
clear_output()
cart.append(item)
print("item {} has been added".format(item))
#create function remove item from the cart
def remove(item):
clear_output()
try:
if item.isdigit():
item_removed = cart.pop(int(item)-1)
print("item {} has been removed".format(item_removed))
else:
cart.remove(item)
print('{} has been removed.'.format(item))
except:
print("Sorry, we can't remove the item".format(item_removed))
#create function to show item in the cart
def show():
clear_output()
if cart:
print("Here is your cart: ")
for i in range(0, len(cart)):
print("{}) {}".format(i+1, cart[i]))
else:
print("Your cart is empty.")
#create function to clear the cart
def clearCart():
clear_output()
cart.clear()
print("Your cart is empty.")
#create main function for loop until user quits
def main():
done = False
while not done:
ans = input("quit/add/remove/show/clear/save : ").lower()
#base case
if ans == "quit":
print("Thanks for using our program.")
show()
done = True
elif ans == "add":
item = input("What would you like to add? ").title()
add(item)
elif ans == "remove":
show()
item = input("What would you like to remove? ").title()
remove(item)
elif ans == "show":
show()
elif ans == "clear":
clearCart()
elif ans == "save":
saveCart(cart)
else:
print("Sorry, that wasn't an option")
#main() #run the program