Write the functiongetInRange (x, a, b)that takes 3 float values and forces to lie between an and is not necessarily less than. If x is between the two bounds, it is returned unmodified. Otherwise, if x is less than the lower bound, return the lower bound, or if xis greater than the upper bound, return the upper bound. Use pythons as a programming language

Respuesta :

Answer:

Here is the getInRange() function:

def getInRange(x,a,b):  

   lower = min(a,b)

   upper = max(a,b)

   if(x<lower):

       x = lower

   elif(x>upper):

       x = upper

   return x

Explanation:

Here is the complete program if you want to take the input float values for x, a and b from user:

x = float(input("Enter x: "))  #prompts user to input float value of x

a = float(input("Enter a: "))  #prompts user to input float value of a

b = float(input("Enter b: "))  #prompts user to input float value of b

def getInRange(x,a,b):  #function definition

   lower = min(a,b)  #computes minimum of a and b and store it to lower

   upper = max(a,b)  #computes maximum of a and b and store it to upper

   if(x<lower):  #if value of x is less than lower bound

       x = lower  # return the lower bound by assigning the value of lower to x

   elif(x>upper):  #if value of x is greater than upper bound

       x = upper  #return the upper bound by assigning value of upper to x

   return x      #function returns the value of x

print(getInRange(x,a,b))  #calls function by passing values for x, a and b and print the result on output screen

   

I will explain this function with an example:

Suppose

x = 1.1

a = 3.3

b = 5.6

Now the method getInRange takes these three values as parameter and works as follows:

lower = min(a,b)  

This statement computes the minimum value of a and b by using min() method and sets the resultant value to lower variable. This becomes:

lower = min(3.3,5.6)

Since 3.3 is less than 5.6 so minimum value is 3.3 Hence

lower = 3.3

upper = max(a,b)

This statement computes the maximum value of a and b by using max() method and sets the resultant value to upper variable. This becomes:

upper= max(3.3,5.6)

Since 5.6 is greater than 3.3 so maximum value is 5.6 Hence

upper = 5.6

if(x<lower):

This statement checks if the value of x is less than value of lower

Since x = 1.1 and lower = 3.3 So, 1.1 is less than 3.3

This means the statement inside this if statement executes:

x = lower

So the value of lower is set to x, So

x = 3.3

Now the elif part will not execute. Program moves to the following statement:

return x

This will return the value of x i.e. 3.3

print(getInRange(x,a,b))    

This statement calls getInRange() method and prints the result on screen. So the output of the above program with input is:

Enter x: 1.1                                                                                                                    Enter a: 3.3                                                                                                                    Enter b: 5.6                                                                                                                  3.3

Ver imagen mahamnasir
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE