-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
55 lines (41 loc) · 1.18 KB
/
calc.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
import sys
import operator
from prompt_toolkit.shortcuts import clear as raw_clear
CALCULATOR_WELCOME_MESSAGE = "Ergonomica Calculator v0.0.1-alpha.1"
try:
input = raw_input
except NameError:
pass
stack = []
operators = {'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'*': lambda x, y: x * y,
'/': lambda x, y: x / y,
'^': lambda x, y: x ** y,
'%': lambda x, y: x % y}
def calc(argc):
"""Calc: a simple RPN calculator for Ergonomica.
Usage:
calc
"""
raw_clear()
print(CALCULATOR_WELCOME_MESSAGE)
while True:
i = input()
if i in operators:
try:
y,x = stack.pop(), stack.pop()
z = operators[i](x,y)
except IndexError:
print("End of stack!")
else:
try:
z = float(i)
except ValueError:
print("Invalid number")
continue
stack.append(z)
raw_clear()
print(CALCULATOR_WELCOME_MESSAGE)
print("\n".join([str(x) for x in stack]))
exports = {"calc": calc}