-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpn.py
More file actions
44 lines (35 loc) · 673 Bytes
/
rpn.py
File metadata and controls
44 lines (35 loc) · 673 Bytes
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
stack = []
def add():
b = stack.pop()
a = stack.pop()
stack.append(a + b)
def subtract():
b = stack.pop()
a = stack.pop()
stack.append(a - b)
def multiply():
b = stack.pop()
a = stack.pop()
stack.append(a * b)
def divide():
b = stack.pop()
a = stack.pop()
stack.append(a / b)
operations = {
"+" : add,
"-" : subtract,
"*" : multiply,
"/" : divide
}
while True:
inpt = input()
try:
inpt = float(inpt)
stack.append(inpt)
except:
try:
inpt = operations[inpt]()
print(stack[-1])
except:
raise ValueError
print(stack)