Feb-18-2017, 06:03 AM
can I use
def A(var): var=True A(var) Print(var)
Output:True
If not,what do I do?
How to turn variable true and false using function?
|
Feb-18-2017, 06:03 AM
can I use
def A(var): var=True A(var) Print(var) If not,what do I do?
Feb-18-2017, 07:01 AM
What do you mean by turn a variable true and false, it can't be both.
Your function does not return any thing at the moment so it's result will book None. What does your functions input look like and what do you expect it's output to be?
I believe hsunteik was asking if he can assign True to a variable within a function, and keep that value (globally) also after function completed execution.
If that is what you meant then yes, it will work. Just make sure of course, regarding your example snippet, that you have "var" declared beforehand.
You can't because Boolean variables are immutable. If you need a function to change the value variable, use a return statement, and assign the return value to the variable.
def longerThatFive(s): return len(s)>5 t1=longerThanFive('abc') t2=longerThanFive('abcdef') print t1,t2
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net def toggle_bool(var): return not var if __name__ == '__main__': myvar = True for i in range(5): myvar = toggle_bool(myvar) print('iteration: {}, var = {}'.format(i, myvar))results:
Feb-20-2017, 11:44 AM
Yes,that is what I meant .
Thanks |
|