Python Forum
How to turn variable true and false using function?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to turn variable true and false using function?
#1
can I use
def A(var):
    var=True
A(var)
Print(var)
Output:
True
If not,what do I do?
Reply
#2
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?
Reply
#3
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.
Reply
#4
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
Reply
#5
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:
Output:
iteration: 0, var = False iteration: 1, var = True iteration: 2, var = False iteration: 3, var = True iteration: 4, var = False
Reply
#6
Yes,that is what I meant .
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 629 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,276 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 921 Aug-07-2023, 05:58 PM
Last Post: Karp
  If function is false search next file mattbatt84 2 1,143 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,037 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,879 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,324 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,203 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  passing variable to function Rejoice 4 2,864 Sep-11-2020, 03:27 AM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020