/
shopping_cart.py
68 lines (52 loc) · 1.47 KB
/
shopping_cart.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
#!/bin/python3
import math
import os
import random
import re
import sys
class Item:
name = ''
price= 0
def __init__(self,s,p):
self.name = s
self.price = p
pass
class ShoppingCart:
cart_names = []
cart_prices= []
tot = 0
def __init__(self):
super().__init__()
def add(self, item):
self.cart_names.append(item.name)
self.cart_prices.append(item.price)
def total(self):
self.tot = sum(self.cart_prices)
return self.tot
def __len__(self):
return len(self.cart_names)
pass
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
items = []
for _ in range(n):
name, price = input().split()
item = Item(name, int(price))
items.append(item)
cart = ShoppingCart()
q = int(input())
for _ in range(q):
line = input().split()
command, params = line[0], line[1:]
if command == "len":
fptr.write(str(len(cart)) + "\n")
elif command == "total":
fptr.write(str(cart.total()) + "\n")
elif command == "add":
name = params[0]
item = next(item for item in items if item.name == name)
cart.add(item)
else:
raise ValueError("Unknown command %s" % command)
fptr.close()