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

Store function ouput if output isnt false (in a single call)

Is there a way to store the output of the function but only if it isnt false, and not having to call the function 2 times as in the example?

def example(x):
    if x>=5:
        return "great"
    else: 
        return False

def main():
    x = 5
    if example(x):
        asnwer = example(x)
    else: 
        print("x<5")

main()

>Solution :

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

def main():
    x = 5
    result = example(x)
    asnwer = result if result else print("x<5")     

You could also just modify the function:

def example(x):
    return "Great" if x>=5 else print("x<5")

def main():
    x = 5
    asnwer = example(x)

main()

This would give asnwer == None when x<5 and asnwer == Great when x>=5.

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