--

A function can be defined as a block of organized, reusable code that is used to perform a single, related action. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments are placed within these parentheses.

The eval() function is used to evaluate the specified expression. If the expression is a correct Python statement, it will be executed. The code is to return the product of two arguments if they are numbers and return “Error: invalid argument!” If they are non numbers. For the second task, the code is to return the result of the subtraction of the two arguments and return “Error: invalid argument!” If they are non numbers.

On jupyter notebook, I built a function named multiply (def multiply) that accepts two input arguments (value1) and (value2) such that if any of the inputs are non-numbers, the function should return “Error:invalid argument”; Else the function should return the product of the two inputs(since the input is a float or integer).

!=input function

i.e if type(value1 or value2) != (float or int)

return “Error:invalid argument”

else:

return (value1 * value2)

To ensure that when the input is run, the user is asked to input the first and second value he wants to multiply, I typed in print (“input the first number you want to multiply: “)

a = eval(input())

For the first value

and

print (“input the first number you want to multiply: ”)

b = eval(input())

Multiply(a, b)

For the second value.

The command produces a prompt that requires the user to input the first and second value he wishes to multiply.

I imputed 4 and 2 respectively and this returned the product as 8 since the argument of the function type is an integer hence it will not return “Error invalid argument”!.

I built a function named subtract (def subtract) that accepts two input arguments (value1) and (value2) such that if any of the input is a float or an integer, the function should return the result of the subtraction of the first value from the second; Else the function will return “Error:invalid argument”

i.e if type(value1 or value2) != (float or int)

return “Error:invalid argument”

else:

return (value1 — value2)

To ensure that when the input is run, the user is asked to input the first and second value he wants to subtract, I typed in

print (“input the first number you want to subtract: “)

a = eval(input())

For the first value

And

print (“input the second number you want to subtract: “)

b= eval(input())

Subtract(a, b).

The command produces a prompt that requires the user to input the first value and the second value he wishes to subtract it from.

I imputed 6 and 4 respectively and this returned 2; since the argument of the function type is an integer hence it will not return “ Error, invalid argument!”

--

--

Oladimeji Ewumi
Solution and problem solving process in Python Function

Freelance Healthcare Writer || Clinical Editor || Researcher || Tech and Innovation Enthusiast ||