-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.py
More file actions
352 lines (244 loc) · 9.58 KB
/
Copy pathcalc.py
File metadata and controls
352 lines (244 loc) · 9.58 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import math, random, string
# Sub Functions
## Sub Calculator Functions
def basic_calculation(equation):
parts = equation.split()
if len(parts) != 3:
print("Equation must have the format 'number operator number'")
return
if "/" in parts[0]:
numerator, denominator = parts[0].split("/")
if not (numerator.isdigit() and denominator.isdigit()):
print("Numerator and denominator of the fraction must be valid numeric values.")
return
parts[0] = str(float(numerator) / float(denominator))
if "/" in parts[2]:
numerator, denominator = parts[2].split("/")
if not (numerator.isdigit() and denominator.isdigit()):
print("Numerator and denominator of the fraction must be valid numeric values.")
return
parts[2] = str(float(numerator) / float(denominator))
if not (parts[0].replace('.', '', 1).isdigit() and parts[2].replace('.', '', 1).isdigit()):
print("Numbers must be valid numeric values.")
return
if parts[1] not in "+-*/":
print("Operator must be one of: +, -, *, /")
return
result = float
if parts[1] == "+":
result = float(parts[0]) + float(parts[2])
elif parts[1] == "-":
result = float(parts[0]) - float(parts[2])
elif parts[1] == "*":
result = float(parts[0]) * float(parts[2])
elif parts[1] == "/":
result = float(parts[0]) / float(parts[2])
print(f"Result: {result}")
def complex_calculation(equation):
equation = equation.replace("pow", "math.pow")
equation = equation.replace("sqrt", "math.sqrt")
result = eval(equation)
print(f"Result: {result}")
## Sub Shape Area Functions
def circle_area(shape_type):
radius = input("Radius: ")
if not radius.replace('.', '', 1).isdigit():
print("Radius must be a valid numeric value.")
return
radius = float(radius)
area = math.pi * radius ** 2
print(f"Area: {area}")
def square_area(shape_type):
length = input("Length: ")
if not length.replace('.', '', 1).isdigit():
print("Length must be a valid numeric value.")
return
length = float(length)
area = length ** 2
print(f"Area: {area}")
def rectangle_area(shape_type):
length = input("Length: ")
width = input("Width: ")
if not (length.replace('.', '', 1).isdigit() and width.replace('.', '', 1).isdigit()):
print("Length and width must be valid numeric values.")
return
length = float(length)
width = float(width)
area = length * width
print(f"Area: {area}")
def cube_area(shape_type):
length = input("Length: ")
width = input("Width: ")
height = input("Height: ")
if not (length.replace('.', '', 1).isdigit() and width.replace('.', '', 1).isdigit() and height.replace('.', '', 1).isdigit()):
print("Length, width, and height must be valid numeric values.")
return
length = float(length)
width = float(width)
height = float(height)
volume = length * width * height
print(f"Volume: {volume}")
# Main Functions
def calculator():
print("Calculation Types: [1] Basic [2] Complex")
calculation_type = input("Calculation Type: ")
if not calculation_type.isdigit():
print("Type must be a number.")
return
calculation_type = int(calculation_type)
if calculation_type == 1:
equation = input("Equation: ")
if equation.lower() in ["quit", "exit"]:
quit()
else:
basic_calculation(equation)
elif calculation_type == 2:
equation = input("Equation: ")
if equation.lower() in ["quit", "exit"]:
quit()
else:
complex_calculation(equation)
def temperature():
degrees = input("Degrees: ")
if not degrees.isdigit():
print("Degrees must be a number.")
return
degrees = int(degrees)
scale = input("Scale (To): ").lower()
if scale not in ["f", "c", "k", "r", "fahrenheit", "celcius", "kelvin", "rankine"]:
print("Scale must be one of: f, c, k, r, fahrenheit, celcius, kelvin, rankine")
return
result = int
if scale in ["f", "fahrenheit"]:
result = int((degrees * 1.8) + 32)
elif scale in ["c", "celcius"]:
result = int((degrees - 32) * 0.5)
elif scale in ["k", "kelvin"]:
result = int(degrees + 273.15)
elif scale in ["r", "rankine"]:
result = int((degrees + 459.67) * 1.8)
print(f"Result: {result}")
def unit_conversion():
value = input("Value: ")
if not value.replace('.', '', 1).isdigit():
print("Value must be a valid numeric value.")
return
value = float(value)
unit_from = input("From Unit (in, cm): ").lower()
if unit_from not in ["in", "cm", "inch", "centimeter", "centimeters"]:
print("From unit must be one of: in, cm, inch, centimeter, centimeters")
return
unit_to = input("To Unit (in, cm): ").lower()
if unit_to not in ["in", "cm", "inch", "centimeter", "centimeters"]:
print("To unit must be one of: in, cm, inch, centimeter, centimeters")
return
result = float
if unit_from in ["in", "inch"]:
if unit_to in ["cm", "centimeter", "centimeters"]:
result = value * 2.54
elif unit_from in ["cm", "centimeter", "centimeters"]:
if unit_to in ["in", "inch"]:
result = value / 2.54
print(f"Result: {result}")
def shape_area():
print("Shape Types: [1] Circle [2] Square [3] Rectangle [4] Cube")
shape_type = input("Shape Type: ")
if not shape_type.isdigit():
print("Type must be a number.")
return
shape_type = int(shape_type)
if shape_type not in [1, 2, 3, 4]:
print("Invalid shape type.")
return
if shape_type == 1:
circle_area(shape_type)
elif shape_type == 2:
square_area(shape_type)
elif shape_type == 3:
rectangle_area(shape_type)
elif shape_type == 4:
cube_area(shape_type)
def string_generation():
print("Select an operation: [1] Generate random integer [2] Generate random float [3] Generate random string [4] Generate random boolean")
operation = input("Operation: ")
if not operation.isdigit():
print("Operation must be a number.")
return
operation = int(operation)
if operation == 1:
lower_bound = input("Lower bound: ")
upper_bound = input("Upper bound: ")
if not (lower_bound.isdigit() and upper_bound.isdigit()):
print("Bounds must be valid numeric values.")
return
lower_bound = int(lower_bound)
upper_bound = int(upper_bound)
if lower_bound > upper_bound:
print("Lower bound cannot be greater than upper bound.")
return
result = random.randint(lower_bound, upper_bound)
print(f"Result: {result}")
elif operation == 2:
lower_bound = input("Lower bound: ")
upper_bound = input("Upper bound: ")
if not (lower_bound.replace('.', '', 1).isdigit() and upper_bound.replace('.', '', 1).isdigit()):
print("Bounds must be valid numeric values.")
return
lower_bound = float(lower_bound)
upper_bound = float(upper_bound)
if lower_bound > upper_bound:
print("Lower bound cannot be greater than upper bound.")
return
result = random.uniform(lower_bound, upper_bound)
print(f"Result: {result}")
elif operation == 3:
length = input("Length of string: ")
if not length.isdigit():
print("Length must be a number.")
return
length = int(length)
chars = string.ascii_letters + string.digits
result = ''.join(random.choice(chars) for _ in range(length))
print(f"Result: {result}")
elif operation == 4:
result = random.choice([True, False])
print(f"Result: {result}")
else:
print("Invalid operation.")
def coordinate_distance():
first_point = input("Enter the coordinates of point 1 (in x,y,z format): ")
second_point = input("Enter the coordinates of point 2 (in x,y,z format): ")
x1, y1, z1 = map(int, first_point.split(","))
x2, y2, z2 = map(int, second_point.split(","))
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)
print(f"The distance between the two points is: {distance}")
def quit():
print("Goodbye!")
return
def main():
print("Hello. Input a response (ex: exit).")
print("Choices: [1] Equation [2] Temperature [3] Unit Conversion [4] Shape Area/Volume [5] String Generation [6] Coordinate Distance [7] Exit")
response = input("Reponse: ")
if response.lower() in [7, "quit", "exit"]:
quit()
if not response.isdigit():
print("Reponse must be a number.")
return
response = int(response)
if response not in [1, 2, 3, 4, 5, 6, 7]:
print("Invalid response.")
return
if response == 1:
calculator()
elif response == 2:
temperature()
elif response == 3:
unit_conversion()
elif response == 4:
shape_area()
elif response == 5:
string_generation()
elif response == 6:
coordinate_distance()
if __name__ == "__main__":
main()