question archive Write functions with docstring for the tasks described below: a) Computing the larger of two integers
Subject:Computer SciencePrice:5.86 Bought12
Write functions with docstring for the tasks described below:
a) Computing the larger of two integers.
b) Computing the smallest of three floating-point numbers.
Python code:
def largestOf2Numbers(a,b): ''' This function takes 2 integer values as argument, find the largest among the 2 numbers and display output ''' m = 0 if a > b: m = a else: m = b print("Largest of",a,"and",b,"is",m) def smallestOf3Numbers(x,y,z): ''' This function takes 3 float point values as argument, find the smallest among them and display the output ''' small = 0 if x < y: if x < z: small = x else: small = z else: if y < z: small = y else: small = z print("Smallest of",x,",",y,", and",z,"is",small) #main method num1 = 34 num2 = 30 largestOf2Numbers(num1,num2) #calling the function fpn1 = 2.45 fpn2 = 1.45 fpn3 = 6.32 smallestOf3Numbers(fpn1,fpn2,fpn3) #calling the function
Step-by-step explanation
Here I have defined 2 functions, largestOf2Numbers() and smallestOf3Numbers().
In the function largestOf2Numbers(), I have used the if-else condition to find out the larger element and stored it in a variable, and then displayed the result.
In the function smallestOf3Numbers(), I have used nested if-else condition to find out the smallest of 3 numbers and stored the value into a variable and then displayed the result.
Now as we know that in Python, anything written outside the function body is treated as part of the main method, so in the main method I have declared 2 variables and pass them as arguments for the largestOf2Numbers() function. Then I have declared 3 more new variables and passed them as an argument for the smallestOf3Numbers() function.
Given below is the screenshot for better understanding:
Please see the attached file for the complete solution