-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
39 lines (33 loc) · 1.1 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
def calculate(num1, num2, operation):
if operation == '+':
return num1 + num2
elif operation == '-':
return num1 - num2
elif operation == '*':
return num1 * num2
elif operation == '/':
if num2 != 0:
return num1 / num2
else:
raise ValueError("Error: Division by zero")
else:
raise ValueError("Error: Unsupported operation")
def main():
print("Simple Calculator")
while True:
try:
num1 = float(input("Enter first number: "))
operation = input("Enter operation (+, -, *, /): ")
num2 = float(input("Enter second number: "))
result = calculate(num1, num2, operation)
print(f"Result: {result}")
continue_calc = input("Do you want to perform another calculation? (yes/no): ")
if continue_calc.lower() != 'yes':
break
except ValueError as e:
print(e)
except Exception as e:
print("An unexpected error occurred:", e)
print("Goodbye!")
if __name__ == "__main__":
main()