-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
46 lines (39 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
40
41
42
43
44
45
46
def calculate(num1, num2, operator):
"""Performs the specified arithmetic operation on two numbers.
Args:
num1: The first number.
num2: The second number.
operator: The arithmetic operator (+, -, *, or /).
Returns:
The result of the operation.
"""
if operator == "+":
return num1 + num2
elif operator == "-":
return num1 - num2
elif operator == "*":
return num1 * num2
elif operator == "/":
if num2 == 0:
print("Error: Cannot divide by zero.")
return None
else:
return num1 / num2
else:
print("Invalid operator.")
return None
while True:
# Get user input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Choose an operator (+, -, *, /): ")
# Perform calculation
result = calculate(num1, num2, operator)
# Display result
if result is not None:
print(f"The result is: {result}")
# Ask if user wants to continue
choice = input("Do you want to perform another calculation? (y/n): ")
if choice.lower() != "y":
break
print("Calculator closed.")