Answer to Question #329094 in Python for ratul

Question #329094

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


1
Expert's answer
2022-04-21T07:02:53-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS