Repository navigation
Expand file tree
/
Copy pathpython_calculator.py
More file actions
150 lines (131 loc) · 5.21 KB
/
Copy pathpython_calculator.py
File metadata and controls
150 lines (131 loc) · 5.21 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
This module contains a simple calculator class that can perform basic
arithmetic operations on two numbers.
The Calculator class has four methods:
- sum: Adds two numbers
- sub: Subtracts one number from another
- mult: Multiplies two numbers
- div: Divides one number by another
All methods take two arguments of type float and return None.
If any of the arguments is not a float, a TypeError is raised.
If the division by zero is attempted, a ZeroDivisionError is raised.
Example usage:
>>> calculator = Calculator()
>>> calculator.sum(2.5, 3.7)
6.2
>>> calculator.sub(10, 5)
5
>>> calculator.mult(2.5, 3)
7.5
>>> calculator.div(10, 2)
5.0
"""
import sys
class Calculator:
"""A simple calculator class that can perform basic mathematical operations"""
def sum(self, first_number: float, second_number: float) -> None:
"""
Calculates the sum of two numbers and prints the result
Args:
number_1 (float): The first number.
number_2 (float): The second number.
"""
try:
print(f'Result: {first_number} + {second_number} = \
{first_number + second_number}\n')
except (TypeError, ValueError, OverflowError) as exc:
print(f'It was not possible to sum {first_number} and \
{second_number}...\n{type(exc).__name__}: {exc}\n')
def sub(self, first_number: float, second_number: float) -> None:
"""
Calculates the subtraction of two numbers and prints the result
Args:
number_1 (float): The first number.
number_2 (float): The second number.
"""
try:
print(f'Result: {first_number} - {second_number} = \
{first_number - second_number}\n')
except (TypeError, ValueError, OverflowError) as exc:
print(f'It was not possible to subtract {second_number} \
from {first_number}...\n{type(exc).__name__}: {exc}\n')
def mult(self, first_number: float, second_number: float) -> None:
"""
Calculates the multiplication between two numbers and prints the result
Args:
number_1 (float): The first number.
number_2 (float): The second number.
"""
try:
print(f'Result: {first_number}*{second_number} = \
{first_number*second_number}\n')
except (TypeError, ValueError, OverflowError) as exc:
print(f'It was not possible to multiply {first_number} and \
{second_number}...\n{type(exc).__name__}: {exc}\n')
def div(self, first_number: float, second_number: float) -> None:
"""
Calculates the division between two numbers and prints the result
Args:
number_1 (float): The first number.
number_2 (float): The second number.
"""
try:
print(f'Result: {first_number}/{second_number} = \
{first_number/second_number}\n')
except ZeroDivisionError as exc:
print(f'Cannot divide by zero...\n{type(exc).__name__}: {exc}')
except (TypeError, ValueError, OverflowError) as exc:
print(f'It was not possible to divide {first_number} by \
{second_number}...\n{type(exc).__name__}: {exc}\n')
def pow(self, first_number: float, second_number: float) -> None:
"""_summary_
Args:
first_number (float): _description_
second_number (float): _description_
"""
try:
print(f'Result: {first_number}**{second_number} = \
{first_number**second_number}\n')
except (TypeError, ValueError, OverflowError) as exc:
print(f'It was not possible to pow {first_number} by \
{second_number}...\n{type(exc).__name__}: {exc}\n')
def main():
"""Main function called when __name__ == '__main__'"""
try:
number_1 = float(input('\nType the first number: ')\
.replace(',', '.'))
except (TypeError, ValueError, OverflowError) as exc:
print(f'{type(exc).__name__}: {exc}\n')
sys.exit(1)
try:
number_2 = float(input('Type the second number: ')\
.replace(',', '.'))
except (TypeError, ValueError, OverflowError) as exc:
print(f'{type(exc).__name__}: {exc}\n')
sys.exit(1)
operation = int(input(f'\nChoose an option:\
\n1. SUM numbers\
\n2. SUBTRACT {number_2} from {number_1}\
\n3. SUBTRACT {number_1} from {number_2}\
\n4. MULTIPLY numbers\
\n5. DIVIDE {number_1} by {number_2}\
\n6. DIVIDE {number_2} by {number_1}\
\nYour option: '))
print()
calculator = Calculator() # Create a new Calculator instance
if operation == 1:
calculator.sum(number_1, number_2)
elif operation == 2:
calculator.sub(number_1, number_2)
elif operation == 3:
calculator.sub(number_2, number_1)
elif operation == 4:
calculator.mult(number_1, number_2)
elif operation == 5:
calculator.div(number_1, number_2)
elif operation == 6:
calculator.div(number_2, number_1)
else:
print('Invalid option selected...\n')
if __name__ == '__main__':
raise SystemExit(main())