1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
GuDViN [60]
3 years ago
8

Write the functiongetInRange (x, a, b)that takes 3 float values and forces to lie between an and is not necessarily less than. I

f 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

Computers and Technology
1 answer:
OLEGan [10]3 years ago
8 0

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

You might be interested in
You are going to interview a small business owner about creating a database for his sandwich shop and bakery. Make a list of que
sveticcg [70]

Answer:

  1. what is your background? education  and work experience?
  2. Tell about description of your business.
  3. How society will be benefited by our business?
  4. What skills are necessary to run this business?
  5. Do you know who are your competitors?
  6. How will you launch your business in market ? How will you make people aware of your business?
  7. Where do you see your business in upcoming years?
  8. What will you do with the profits?
  9. What if u face all loss in this business any other option?
  10. Do you have employees for this business?

7 0
3 years ago
What does CPL stand for
ohaa [14]
I think it could be Cost Per Lead
4 0
4 years ago
Read 2 more answers
What is a word processing program? Give examples of word processing programs.
Kipish [7]

Answer:

A word processor, or word processing program, does exactly what the name implies. It processes words. It also processes paragraphs, pages, and entire papers. Some examples of word processing programs include Microsoft Word, WordPerfect (Windows only), AppleWorks (Mac only), and OpenOffice.org.

8 0
3 years ago
A) Explain the concept of memory management unit as related to Operating systems.
USPshnik [31]

Answer:

Memory is the faculty of the brain by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action.

Explanation:

4 0
3 years ago
Pros and cons of toyota's organizational structure?
GarryVolchara [31]
In 2013, Toyota changed its organizational structure from the centralized structure to: 
- the Global hierarchy, 
- the Geographic divisions, and 
- the Product-based divisions. 

This change was made to adapt the consumer's demand in each of the regional markets all over the world. The most important element of this structure is the speed of handling issues and problems of all Toyota's branches. However, this structure also has a weakness which is the decreasing of headquarter's control over the global organization.
4 0
4 years ago
Other questions:
  • ____ are not associated with data from the database and are used to display such things as the report's title
    15·1 answer
  • Splunk In most production environments, _______ will be used as the source of data input?
    12·1 answer
  • To locate something in the database, one must type in the keyword into the ________ of the application.
    12·1 answer
  • Which of the following allows the transmission of voice and often video communication over the internet?
    12·1 answer
  • What other kinds of toolbars exist in addition to those that are part of individual programs?
    10·1 answer
  • Help meee pleaseeee
    5·1 answer
  • If you were an architect planning on building a large scale Municipal complex what type of engineer would you identify as essent
    10·1 answer
  • Write a basic notation for
    8·1 answer
  • In the program below, which two variables have the same scope?
    9·2 answers
  • Is anyone excited for the new matrix coming out ?
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!