-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddingwords.py
39 lines (35 loc) · 1.02 KB
/
addingwords.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
#addingwords.py
try:
d = {}
while True:
inp = input().split()
if inp[0]=="clear":
d.clear()
if inp[0] =="def":
d[inp[1]] = inp[2]
if inp[0] == "calc":
unknown = False
eq = []
for c in inp[1:-1]:
if c in d:
eq.append(d[c])
elif c in ("+", "-"):
eq.append(c)
else:
unknown = True
if unknown:
print(" ".join(inp[1:]), "unknown")
else:
res = str(eval(''.join(eq)))
found = False
item = ''
for name, val in d.items():
if res ==val:
found = True
item = name
if found:
print(" ".join(inp[1:]),item)
else:
print(" ".join(inp[1:]),"unknown")
except EOFError as e:
pass