Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

calling on an if and return result from a def outside of the def in python

I am new to python and was wonder how to use the return statement from a def in another def how would I implement that. I know it’s probably an obvious answer but I’m stuck.


    def equal(A):
        if A == 2:
            return ("true")
        if A != 2:
            return "false"

for example, if I wanted to use "true" or "false" to affect something else

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should use the Python truth values True and False as returns. Also you should use if/else rather than two if clauses. So your function should be:

def equal(A):
    if A == 2:
        return True
    else:
        return False # if you get here you know that A does not equal 2

Or more simply:

def equal(A)
    if A == 2:
        return True
    return False # no need for the second clause

To use those values in another function you could do something like

def test(some_parameter)
    answer = equal(some_parameter)
    if answer == True:
        # do one thing
    else: # it is False
        # do something else

I would also strongly recommend you go to The Python Tutorial and work through at least the first six chapters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading