+ 1

How to take a functions output and do something with it in another function ?

I have two functions one gives me the value of x And the other checks if x==4 (for example) and a button its command is the check function I need the second function for the button but it says x is not defined so how How do I make x defined in the second function x is the output of the first?

23rd Jan 2020, 2:20 PM
Pattern
Pattern - avatar
4 Réponses
+ 2
Generally you don't want to use global to make values available in another function - you would want to pass them as arguments to the function that needs it! def f(): x =... some code return x def g(parameter): do something with parameter return parameter Then you can write: my_value = f() my_next_value = g(my_value) Or directly g(f()) or something like that.
23rd Jan 2020, 9:12 PM
HonFu
HonFu - avatar
+ 1
you can use "global" statement which makes a variable available in all scopes. however it is essential to check the variable name you are making global doesn't interrupt any other part of the code seeing as it might have the same name as another variable. you can use global like this: def foo(): global x x = 10 print(x) def bar(): global x print(x) x = 20 print(x) def baz(): global x print(x) foo() # 10 bar() # 10 20 baz() # 20
23rd Jan 2020, 2:32 PM
pooya shams
+ 1
Still gives me x is not defined
23rd Jan 2020, 2:52 PM
Pattern
Pattern - avatar
0
Adding to the answer by pooya shams global x has to be declared outside all the functions also. So just add at the start global x x = (any starting value you want)
23rd Jan 2020, 5:06 PM
XXX
XXX - avatar