-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.py
More file actions
43 lines (37 loc) · 1.34 KB
/
calc.py
File metadata and controls
43 lines (37 loc) · 1.34 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
def calculator():
print("Simple Calculator")
print("Operations: +, -, *, /")
while True:
try:
# Get input from user
num1 = float(input("Enter first number: "))
operation = input("Enter operation: ")
num2 = float(input("Enter second number: "))
# Perform calculation based on operation
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
if num2 == 0:
print("Error: Cannot divide by zero!")
continue
result = num1 / num2
else:
print("Invalid operation!")
continue
# Display result
print(f"Result: {result}")
# Ask if user wants to continue
again = input("Calculate again? (yes/no): ").lower()
if again != 'yes':
print("Thank you for using the calculator!")
break
except ValueError:
print("Please enter valid numbers!")
continue
# Run the calculator
if __name__ == "__main__":
calculator()