-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram9.py
More file actions
24 lines (21 loc) · 781 Bytes
/
program9.py
File metadata and controls
24 lines (21 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 9. Write a program to create a function which accepts 2 numbers and one arithmetic operator. Return the answer accordingly.
def calculate(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 == 0:
return "Error: Division by zero is not allowed."
return num1 / num2
elif operator == '%':
return num1 % num2
else:
return "Error: Invalid operator."
n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))
op = input("Enter operator (+, -, *, /, %): ")
result = calculate(n1, n2, op)
print(f"The result is: {result}")