How do you place one function into another function?


#1

This is my code:

def is_median(func): 
if func (0, 0, 0) != 0: 
return False 

if func (1, 2, 3) != 2: 
return False 

return True 

def median(a, b, c): 
if a <= b <= c or c <= b <= a: 
return(a) 
elif b <= a <= c or c <= a <= b: 
return(b) 
else: 
return(c)

the first function returns True and I need it to return false how can I do so?

The assignment page:
https://cs1110.cs.virginia.edu/w04-tester.html


#2

They're not asking you to write a function inside another one, though there is nothing special about doing so.

def f():
    def g():
        return 'peanut butter'

Note that indentation is significant in Python.

What you're asked to do is to call the argument, again, nothing special about that, it's done the same way as in any other context.


#3

I am sorry I still don't understand.


#4

So I am suppose to be putting median inside is_median?


#5

Go read the instructions and get a better idea of what the purpose of your function is. Then think about what actions would help accomplishing that purpose.

I do not know what you think you mean by putting median inside is_median. But if you think it is something that would help in what your function is supposed to do - then motivate why it would be helpful. Or if you can't motivate it, then ask yourself where that thought is coming from.

For example, if I want to get some piece of soil wet, then filling up a watering can would be a sensible action if the watering can is of a size that I can carry and can hold sufficient amounts of water to wet the soil.

I wouldn't ask "am I supposed to fill a watering can" - because that isn't part of the purpose. If I've seen Fred the farmer use a watering can to wet soil, then that would be where that thought is coming from, and even though I don't see how it helps, I can go ask Fred.

So no, you're not supposed to put median in is_median, you're supposed to test median. If putting median in is_median helps you do that, then yes, it would be a sensible thing to do.

But again, I have no clue what you mean by that, and you very likely will need to think harder yourself about what you mean by it - or at least choose different words in describing that action.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.