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
  not able to call the variable inside the if/elif function mareeswaran 3 505 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  gpiozero button turn off LED that is already on duckredbeard 3 785 Dec-11-2024, 06:23 PM
Last Post: duckredbeard
  Exceeding the value - turn on the relay. stsxbel 0 560 May-27-2024, 07:18 PM
Last Post: stsxbel
  Variable for the value element in the index function?? Learner1 8 2,843 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,514 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 7,067 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 2,073 Aug-07-2023, 05:58 PM
Last Post: Karp
  If function is false search next file mattbatt84 2 1,914 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,780 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 2,878 Feb-09-2022, 10:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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