This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_calculator.py
More file actions
70 lines (55 loc) · 1.68 KB
/
Copy pathSmart_calculator.py
File metadata and controls
70 lines (55 loc) · 1.68 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
print("Welcome into my python based calculator!\n Please note that you should leave a space in between numbers and signs. \n This calculator can add +, rest -, multiply x, divide \ and raise to the power ^. \n")
equation = input ("Please input your equation: \n")
# addition
def add(x, y):
return x + y
# subtraction
def subtract(x, y):
return x - y
# multiplication
def multiply(x, y):
return x * y
# division
def divide(x, y):
return x / y
#def elevate
def elevate(x, y):
return x **y
class Perceptron:
def recognize(self, equation):
#recognize numbers
res = [int(i) for i in equation.split() if i.isdigit()]
num1 = res[0]
num2 = res[1]
#recognize symbol
valid_symbols= "+^-/x"
for element in equation:
if element in valid_symbols:
symbol = element
self.num1 = num1
self.num2 = num2
self.symbol = symbol
#returns
# return num1,num2,symbol
def calculate (self):
num1 = self.num1
num2 = self.num2num2 = self.num2
symbol = self.symbol
for symbol in equation:
#equations
if symbol == "+":
print(num1, "+", num2, "=", add(num1, num2))
elif symbol == "-":
print(num1, "-", num2, "=", subtract(num1, num2))
elif symbol == "x":
print(num1, "x", num2, "=", multiply(num1, num2))
elif symbol == "/":
print(num1, "/", num2, "=", divide(num1, num2))
elif symbol == "^":
print(num1, "^", num2, "=", elevate(num1,num2))
#else:
# print("Sorry ths calculation goes beyond my scope!")
perceptron = Perceptron()
perceptron.recognize(equation)
perceptron.calculate()
print("Thank you for using my calculator!")