-
Notifications
You must be signed in to change notification settings - Fork 2
/
fridge.py
executable file
·81 lines (70 loc) · 1.9 KB
/
fridge.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
#!/usr/bin/python
import json
import sys
fridge = {}
product = ""
action = ""
def main(args):
fridge = load()
if len(args) <= 1:
action = raw_input("Operator = ")
if action == "show":
show_fridge()
return
amount = int(raw_input("Anzahl = "))
product = raw_input("Produkt = ")
elif args[1] == "show":
show_fridge()
return
else:
action = args[1]
amount = int(args[2])
product = args[3]
open_fridge(action, product, amount)
def open_fridge(action, product, amount):
if action == "add":
for i in range(amount):
add_product(product)
print str(amount) + " " + product + " hinzugefuegt"
save(fridge)
elif action == "sub":
if (not(fridge.has_key(product))):
print "erst reinlegen dann raus nehmen!!"
return
for i in range(amount):
sub_product(product)
print str(amount) + " " + product + "rausgenommen"
print str(fridge[product]) + " " + product + "verbleiben"
save(fridge)
else:
print "falsche Eingabe. Bitte nutze \"show\", \"add\" oder \"sub\"."
def show_fridge():
for product in fridge.keys():
print "" + product + " " + str(fridge[product])
def add_product(product):
if (not(fridge.has_key(product))):
fridge.update({product : 0})
add_product(product)
else:
fridge[product] += 1
def sub_product(product):
if fridge[product] >= 1:
fridge[product] -= 1
else:
print "Fridge leer!"
def save(fridge):
f = open("fridge.json", "w")
json.dump(fridge,f)
f.close()
def load():
try:
global fridge
f = open("fridge.json","r")
fridge = json.load(f)
f.close()
except:
fridge = {}
finally:
return fridge
if __name__ == '__main__':
sys.exit(main(sys.argv))