Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Variables in functions
-
I'm having trouble with variables inside functions (created with "def").
For example, here is a really simple code I made:<pre>def first():
star = 3
def second(lego):
print(lego + 1)
first()
second(star)</pre>Instead of the program returning the number 4, it says that star is "not defined". I'm sure this is something simple, but I have NO clue.
Thanks,
Cubbarooney -
For further information read up on variable scopes and namespaces in the reference or tutorial.
Simply put, each variable is bound to a certain scope, global or local. Your star is a local variable of and only of the first function. If you want your star variable to be an argument of your second function, declare star globally. So move 'star = 3' to outside of both functions.
-
The star variable is only valid <em>within</em> the first function, that's called a <em>local</em> variable. If you want to use values that you have in a function outside of that function, you would typically return the value and then assign that return value to another variable, like this:
<pre>def first():
star = 3
return stardef second(lego):
print lego + 1foo = first()
second(foo)</pre>An alternative would be to declare star as a global variable before the first function. In order to modify it within a function, you'd then have to use the global keyword.
<pre>star = 1
def first():
global star
star = 3def second(lego):
print lego + 1first()
second(star)</pre> -
@jugisto and @omz
Thanks to both of you! :D
All three ways work, but not for all things.Just to make sure I understand, in the following code:
<pre>spam = 123
knight = 2
def rubber():
gulcup = 6def chicken():
global knight, parrot
knight = 42
parrot = knight/2</pre>'spam', 'knight', and 'parrot' are global variables while 'gulcup' (pronounced gill-cup, incase you don't get the reference) is a local variable to the function 'rubber()'.
Do note that I do not define parrot until the function 'chicken()'. Based on my experimentation this is fine. If I'm wrong about this (or anything above really) let me know.
Again, thanks for the help (and speedy response. I wish I was that fast)
Cubbarooney
Edit: Also, how do you get the code into those yellow boxes? Are my quotation marks interfering? I took them out now just in case. Take that back, I just noticed that (when I pressed edit) that the quotation marks changed to html
-
You're correct, though I generally wouldn't recommend defining a global variable inside of a function, that can get quite confusing.
-
Hmmm...
Every time I try doing that it doesn't work.Here is some code I just tried:
<pre>global red
blue = input()
def color():
red = blue + 2
color()
print('red', red,)</pre>An error pops up saying "global name 'red' not defined"
Thanks,
Cubbarooney -
you need to tell the function color, that you're referring to the global red
<pre>
global red
blue = input()
def color():
global red
red = blue + 2
color()
print('red', red,)
</pre> -
Lol, ok. So make it exist on the "outside" and then (when used in a defined function) tell the program to use the global variable.
Thanks for all your help!
Cubbarooney