-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Script.py
More file actions
82 lines (74 loc) · 2.74 KB
/
Python Script.py
File metadata and controls
82 lines (74 loc) · 2.74 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
# Calculator Program
import math
def display_menu():
"""Display the calculator menu"""
print("\n" + "="*40)
print(" CALCULATOR")
print("="*40)
print("Select an operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Square Root")
print("="*40)
def get_operation():
"""Get the operation choice from the user"""
while True:
display_menu()
choice = input("Enter your choice (1-5): ").strip()
if choice in ['1', '2', '3', '4', '5']:
return choice
else:
print("Invalid choice. Please enter a number between 1 and 5.")
def get_numbers(operation):
"""Get numbers from the user based on operation type"""
if operation == '5': # Square root only needs one number
while True:
try:
num = float(input("Enter a number: "))
return num, None
except ValueError:
print("Invalid input. Please enter a valid number.")
else: # Other operations need two numbers
while True:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
return num1, num2
except ValueError:
print("Invalid input. Please enter valid numbers.")
def perform_calculation(operation, num1, num2):
"""Perform the selected calculation"""
if operation == '1':
return 'Addition', num1 + num2
elif operation == '2':
return 'Subtraction', num1 - num2
elif operation == '3':
return 'Multiplication', num1 * num2
elif operation == '4':
return 'Division', num1 / num2 if num2 != 0 else "Error: Division by zero"
elif operation == '5':
return 'Square Root', math.sqrt(num1) if num1 >= 0 else "Error: Cannot take square root of negative number"
def display_result(operation_name, num1, num2, result):
"""Display the calculation result"""
print("\n" + "-"*40)
if num2 is not None:
print(f"{operation_name}: {num1} and {num2}")
else:
print(f"{operation_name}: {num1}")
print(f"Result: {result}")
print("-"*40)
def main():
"""Main calculator loop"""
while True:
operation = get_operation()
num1, num2 = get_numbers(operation)
operation_name, result = perform_calculation(operation, num1, num2)
display_result(operation_name, num1, num2, result)
again = input("\nWould you like to perform another calculation? (yes/no): ").strip().lower()
if again not in ['yes', 'y']:
print("Thank you for using the calculator. Goodbye!")
break
if __name__ == "__main__":
main()