-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvance_calculator.py
More file actions
executable file
·47 lines (37 loc) · 1.3 KB
/
advance_calculator.py
File metadata and controls
executable file
·47 lines (37 loc) · 1.3 KB
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
# Script of Advance Calculator using Python using regexp module
# importing the regexp module
import re
# salutation and providing termination condition.
print("Welcome! I am your Advance Calculator.")
print("Type 'quit' to terminate.\n")
#declaring global variables
previous_value = 0
flag = True
# Defining non parametrized function to perform all the operations.
def calculate():
global flag
global previous_value
equation = ""
# Taking input from the user.
if previous_value == 0:
equation = input("Give me something to calculate:")
else:
equation = input(str(previous_value))
# Termination step.
if equation == 'quit':
print("You exit.")
print("See you next time.")
flag = False
else:
equation = re.sub('[a-zA-z,.:()" "]', '', equation)
# For security point of view here we eliminate all the letters and keep only numerical characters, if entered by the user.
# Calculating the results.
if previous_value == 0:
previous_value = eval(equation)
else:
previous_value = eval(str(previous_value) + equation)
# Calling the function within the infinite loop.
# On execution will continue to calculate until the user enters 'quit' .
while flag:
calculate()
#end of the script