-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_Calculator.py
56 lines (46 loc) · 1.62 KB
/
Simple_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
import math
def calculator():
while True:
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Square Root")
print("6. Quit")
choice = input("Enter choice (1/2/3/4/5/6): ")
if choice == '6':
print("Calculator closed.")
break
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if choice == '1':
result = num1 + num2
operator = '+'
elif choice == '2':
result = num1 - num2
operator = '-'
elif choice == '3':
result = num1 * num2
operator = '*'
elif choice == '4':
if num2 == 0:
print("Cannot divide by zero!")
continue
result = num1 / num2
operator = '/'
# Display the calculation result
print(f"{num1} {operator} {num2} = {result}")
elif choice == '5':
num = float(input("Enter a number: "))
if num < 0:
print("Cannot calculate square root of a negative number!")
else:
square_root = math.sqrt(num)
# Display the square root result
print(f"Square root of {num} = {square_root}")
else:
print("Invalid choice!")
# Call the calculator function to start the program
calculator()