Write a python function that will perform the basic calculation (addition, subtraction,
multiplication and division) based on 3 arguments. They are:
Operator ('+', '-', '/', '*')
First Operand (any number)
Second Operand (any number)
Your first task is to take these arguments as user input and pass the values to the function
parameters.
Your second task is to write a function and perform the calculation based on the given operator.
Then, finally return the result in the function call and print the result.
=====================================================
Input:
"+"
10
20
Function Call:
function_name("+", 10, 20)
Output:
30.0
================================
Input:
"*"
5.5
2.5
Function Call:
function_name("*", 5.5, 2.5)
Output:
13.75
def calculation(ch, num1, num2):
result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
else:
print("Input character is not recognized!")
return result
print("Which operation would you like to perform?")
operation = input("Enter any of these char for specific operation +,-,*,/: ")
F_num = int(input("Enter First Number: "))
S_num = int(input("Enter Second Number: "))
res = calculation(operation, F_num, S_num)
print(F_num, operation, S_num, "=", res)
Comments
Leave a comment