-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
217 lines (185 loc) · 11.6 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#NOTE: we will be using functions and loops to minimize the size of the script soon!
from prettytable import PrettyTable
import math
print("=" * 50)
print(" " * 8 + "Welcome to the Enhanced Calculator!" + " " * 8)
print(" " * 14 + "This calculator supports:"+ " " * 14)
print(" " * 15 + "Arithmetic Operations"+ " " * 15)
print(" " * 15 + "Comparison Operations"+ " " * 15)
print(" " * 14 + "Trigonometric Functions"+ " " * 14)
print("=" * 50)
arithmetic_type = input("Do you want to use Arithmetic Operator (Y/N)?: ").title()
if arithmetic_type in ["Y", "Yes", "YES", "Yep", "yep", "Yep Operator", "Yup"]:
operator_type = input("Which Arithmetic Operator do you want to use?: ").title()
result_type = input("Do you want the answer in integer form or float form (I/F)?: ").title()
num1 = int(input("First Number: "))
num2 = int(input("Second Number: "))
if operator_type in ["+", "Addition", "Addition Operator", "Add"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Addition", f"{num1}", f"{num2}", f"{num1} + {num2}", num1 + num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Addition", f"{num1}", f"{num2}", f"{num1} + {num2}", float(num1 + num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
elif operator_type in ["-", "Subtraction", "Subtraction Operator", "Subtract"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Subtraction", f"{num1}", f"{num2}", f"{num1} - {num2}", num1 - num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Subtraction", f"{num1}", f"{num2}", f"{num1} - {num2}", float(num1 - num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
elif operator_type in ["*", "Multiplication", "Multiplication Operator", "Multiply"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Multiplication", f"{num1}", f"{num2}", f"{num1} * {num2}", num1 * num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Multiplication", f"{num1}", f"{num2}", f"{num1} * {num2}", float(num1 * num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
elif operator_type in ["/", "Division", "Division Operator", "Divide"]:
if num2 != 0:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Division", f"{num1}", f"{num2}", f"{num1} // {num2}", num1 // num2]) # Integer division
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Division", f"{num1}", f"{num2}", f"{num1} / {num2}", float(num1 / num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
else:
print("Undefined (division by zero)")
elif operator_type in ["%", "Modulus", "Modulus Operator", "Mod"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Modulus", f"{num1}", f"{num2}", f"{num1} % {num2}", num1 % num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Modulus", f"{num1}", f"{num2}", f"{num1} % {num2}", float(num1 % num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
elif operator_type in ["**", "Exponent", "Exponent Operator", "Exp"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Exponent", f"{num1}", f"{num2}", f"{num1} ** {num2}", num1 ** num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Exponent", f"{num1}", f"{num2}", f"{num1} ** {num2}", float(num1 ** num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
elif operator_type in ["//", "Floor Division", "Floor Division Operator", "Floor Div"]:
if result_type in ["I", "i", "Integer", "Integer Form", "Int"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Floor Division", f"{num1}", f"{num2}", f"{num1} // {num2}", num1 // num2])
print(table)
elif result_type in ["F", "f", "Float", "Float Form", "Float"]:
table = PrettyTable(["Arithmetic Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Floor Division", f"{num1}", f"{num2}", f"{num1} // {num2}", float(num1 // num2)])
print(table)
else:
print("Invalid result type, Try again later :)")
exit()
else:
print("Invalid operator, Try again later :)")
exit()
elif arithmetic_type in ["N", "n", "No", "no", "NO", "Nope", "nope"]:
comparison_type = input("Do you want to use Comparison Operator (Y/N)?: ").title()
if comparison_type in ["Y", "Yes", "YES", "Yep", "yep", "Yep Operator", "Yup"]:
operator_type = input("Which Comparison Operator do you want to use?: ").title()
num1 = int(input("First Number: "))
num2 = int(input("Second Number: "))
if operator_type in ["==", "Equal", "Equal Operator", "Eq"]:
result = "True" if num1 == num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Equal Operator", f"{num1}", f"{num2}", f"{num1} == {num2}", result])
print(table)
elif operator_type in [">", "Greater", "Greater Operator", "Gt"]:
result = "True" if num1 > num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Greater Operator", f"{num1}", f"{num2}", f"{num1} > {num2}", result])
print(table)
elif operator_type in ["<", "Less", "Less Operator", "Lt"]:
result = "True" if num1 < num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Less Operator", f"{num1}", f"{num2}", f"{num1} < {num2}", result])
print(table)
elif operator_type in [">=", "Greater Equal", "Greater Equal Operator", "Geq"]:
result = "True" if num1 >= num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Greater Equal Operator", f"{num1}", f"{num2}", f"{num1} >= {num2}", result])
print(table)
elif operator_type in ["<=", "Less Equal", "Less Equal Operator", "Leq"]:
result = "True" if num1 <= num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Less Equal Operator", f"{num1}", f"{num2}", f"{num1} <= {num2}", result])
print(table)
elif operator_type in ["!=", "Not Equal", "Not Equal Operator", "Neq"]:
result = "True" if num1 != num2 else "False"
table = PrettyTable(["Comparison Operator", "First Number", "Second Number", "Solving", "Result"])
table.add_row(["Not Equal Operator", f"{num1}", f"{num2}", f"{num1} != {num2}", result])
print(table)
else:
print("Invalid operator, Try again later :)")
exit()
elif comparison_type in ["N", "n", "No", "no", "NO", "Nope", "nope"]:
trigonometry_operator = input("Do you want to use trigonometric expressions (Y/N)?: ").title()
if trigonometry_operator in ["Y", "Yes", "YES", "Yep", "yep", "Yep Operator", "Yup"]:
operator_type = input("Which trigonometric operator do you want to use?: ").title()
num1 = float(input("Angle (in degrees): "))
angle_rad = math.radians(num1)
if operator_type in ["Sin", "Sine", "Sine Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Sin(ϴ)", f"{num1}", math.sin(angle_rad)])
print(table)
elif operator_type in ["Cos", "Cosine", "Cosine Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Cos(ϴ)", f"{num1}", math.cos(angle_rad)])
print(table)
elif operator_type in ["Tan", "Tangent", "Tangent Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Tan(ϴ)", f"{num1}", math.tan(angle_rad)])
print(table)
elif operator_type in ["Tanh", "Tanh Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Tanh(ϴ)", f"{num1}", math.tanh(angle_rad)])
print(table)
elif operator_type in ["Sinh", "Sine Hyperbolic", "Sine Hyperbolic Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Sinh(ϴ)", f"{num1}", math.sinh(angle_rad)])
print(table)
elif operator_type in ["Cosh", "Cosine Hyperbolic", "Cosine Hyperbolic Operator"]:
table = PrettyTable(["Trigonometric Operator", "Angle (degrees)", "Result"])
table.add_row(["Cosh(ϴ)", f"{num1}", math.cosh(angle_rad)])
print(table)
else:
print("Invalid operator, Try again later :)")
exit()
elif trigonometry_operator in ["N", "n", "No", "no", "NO", "Nope", "nope"]:
print("Then why are you using the calculator ( -.-)?")
exit()
else:
print("Invalid input, Try again later :)")
else:
print("Invalid input, Try again later :)")