Trying to use a returned value without the other parts of a function [closed]

You can add in a function parameter that allows you to print out within the function or not:

def GetNumber(print_num=False):
    number = 45

    if print_num:
        print(number)

    return number

GetNumber(True)

num = GetNumber()

print("my num is: {}\n".format(num));

GetNumber(True) will print out the number from within the function, whereas GetNumber() will simply return the number without printing it. Note that in my example above, it returns the number regardless of how print_num is set. You can use it, or simply ignore it.

We set print_num=False. That means that if the parameter isn’t sent in, we set a default value of False to it.

Leave a Comment