-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram10.py
More file actions
31 lines (25 loc) · 912 Bytes
/
program10.py
File metadata and controls
31 lines (25 loc) · 912 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
25
26
27
28
29
30
31
# 10.Write a program to create 4 lambda functions which shall accept 2 numbers and one arithmetic operator. As per arithmetic operator related lambda functions shall be invoked.
add = lambda x, y: x + y
sub = lambda x, y: x - y
mul = lambda x, y: x * y
div = lambda x, y: x / y if y != 0 else "Error: Division by zero"
operations = {
"+": add,
"-": sub,
"*": mul,
"/": div
}
def calculator():
try:
num1 = float(input("Enter First number: "))
num2 = float(input("Enter Second number: "))
op = input("Enter operator (+, -, *, /): ")
if op in operations:
result = operations[op](num1, num2)
print(f"Result: {result}")
else:
print("Invalid operator!")
except ValueError:
print("Please enter valid numeric values.")
if __name__ == "__main__":
calculator()