-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator - 22.03.2018.py
92 lines (74 loc) · 2.27 KB
/
calculator - 22.03.2018.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
82
83
84
85
86
87
88
89
90
##########################
##### CALCULATOR #####
##########################
'''Program by Piotr Gołdyś'''
# Imports:
import re
import time
########
run = True
ans = 0
equation = ''
safe = 0
history = ''
#Welcome message / instructions
print('##########################')
print('##### CALCULATOR #####')
print('##########################')
print()
print('Hello, human. \nType some calculations!')
print()
print("(HISTORY - type 'h'")
print("(RESTART - type 'r'")
print("( EXIT - type 'e'")
print("For 'sqrt' use '**(1/2)'")
print('--------------------------------------------------------------------')
def check(): #Checks for special/wrong input
global ans, run, equation, safe, history
equation = input()
if equation == 'e': #Exit
print('Goodbye')
run = False
elif equation == 'r': #Restart
ans = 0
safe = 0
history = ''
equation = ''
elif equation == 'h': #History
print('History: ', history)
else: #Checks for wrong inputs and protects from using eval()'s function safe loop
notsafe = 0
for x in equation:
if set('[,.;:{}[&%$@!~]').intersection(equation) or x.isalpha():
equation = re.sub('[a-zA-Z,.;:{}[&%$@!~]', ' ', equation) #Deletes special/dangerous characters
safe = 0
notsafe = 1
else:
safe = 1
if notsafe == 1:
print('Wrong input!')
time.sleep(2)
def perform(): #Do maths
global history, run, ans, equation, safe
if safe == 1:
equation = re.sub('[a-zA-Z,.;:{}[&%$@!~]', ' ', equation)
if ans == 0: #For the first equation
if equation[0] == '*' or equation[0] == '/':
ans = str(eval('0' + equation))
else:
ans = str(eval(equation))
history += equation + ' '
else: #For next equations
if equation[0].isdigit():
print('Wrong input!')
time.sleep(2)
else:
ans = str(eval(ans + equation))
history += equation + ' '
if run == True:
print(ans, end='') #Printing stuff
def program():
check()
perform()
while run:
program()