This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
executable file
·133 lines (104 loc) · 3.66 KB
/
calculator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os.path
import psycopg2
import re
import db_connector_alchemy as session
from models import Results, Users
class Calculator:
actions = ['+', '-', '/', '*']
def __init__(self, first_number, second_number, action):
self.first_number = first_number
self.second_number = second_number
self.action = action
def addition(self):
return self.first_number + self.second_number
def subtraction(self):
return self.first_number - self.second_number
def multiplication(self):
return self.first_number * self.second_number
def division(self):
return self.first_number / self.second_number
def calculate(self):
if self.action == '+':
return self.addition()
elif self.action == '-':
return self.subtraction()
elif self.action == '*':
return self.multiplication()
elif self.action == '/' and self.second_number != 0:
return self.division()
elif self.action == '/' and self.second_number == 0:
result = None
return result
def check_input_num(number):
if number.isdigit():
return float(number)
else:
print("Please try again")
return False
def check_wish(wish):
if wish == 'y':
session.read_res()
return False
elif wish == 'n':
return False
else:
print('Try again please! ')
return True
def user_input():
our_user = input("Please enter your name > ")
return our_user
def parse_expression(my_string):
my_regex = r'(^-?\d+[.]?\d+|^-?\d+)([{}])(-?\d+[.]?\d+$|-?\d+$)'.format(''.join(Calculator.actions))
result = re.match(my_regex, my_string)
if result:
first_number = float(result.group(1))
action = result.group(2)
second_number = float(result.group(3))
return first_number, second_number, action
else:
print("Try again")
return False
if __name__ == "__main__":
table_name = 'calculator'
again = 'y'
while again == 'y':
alchemy_actions = session.AlchemyActions()
our_user = user_input()
alchemy_actions.output_user_actions_count(our_user)
our_user_id = alchemy_actions.user_id(our_user)
if alchemy_actions.user_in_table(our_user):
alchemy_actions.update_counter(our_user)
else:
our_user_to_table = Users(our_user, 1)
alchemy_actions.add_user(our_user_to_table)
print('Do you want to see history? (y/n)')
var = 1
while var:
var = check_wish(input())
print("Please input first number, action and second number: ")
first_number = False
while not first_number:
my_string = input()
if parse_expression(my_string):
first_number, second_number, action = parse_expression(my_string)
break
else:
continue
our_example = Calculator(first_number, second_number, action)
if our_example.calculate():
print("Your result is: ", round(our_example.calculate(), 4))
to_alchemy_results = Results(first_number, action, second_number,
our_example.calculate(), our_user_id)
alchemy_actions.add_res(to_alchemy_results)
else:
print("It's not possible to divide by zero!")
print("Do you want to continue? (y/n)")
while True:
again = (input())
if again == "y":
break
elif again == "n":
break
else:
print("Try again !")
continue