Python and global variables


#1

I could be incorrect on this but I noticed when I define a variable outside a function and to change the variable you need to use the key word ‘global’ but it does not seem to work…

code would look something like,

foo = "foo"
def foo():
    global foo
    foo = "bla"
    self.say(foo)
def bla():
    self.say(foo)
self.say(foo)
foo()
bla()
self.say(foo)

Edit: I found a way around what I needed, just made returns values to and from what I needed…


I seem to fail at all fronts to protect Reynaldo
#2

Ah yeah, it seems our Python parser, Filbert, doesn’t support the global keyword yet. You can check which other slightly-more-advanced Python features are or aren’t yet included on Filbert’s readme: https://github.com/codecombat/filbert


I need help to change this code so it could store enemy health predictions and command units to attack effectively
#3

Now I have to code the whole damn thing in Javascript all over again :cry: :pensive: :cry: WHY U DO THIS NICK
Mod edit: Please don’t swear


#4

Is there a solution to global variables in python yet? Or do we have to pass things back and forth?


#5

This thread is over a year old. Try not to revive dead threads, but it’s always good to keep in check in case of updates :slightly_smiling_face:


#6

Sorry, I’m new. So you’re saying that it’s better to create a new thread asking the same question than to revive the most relevant thread?

"Ah yeah, it seems our Python parser, Filbert, doesn’t support the global keyword yet. "

Seems like pinging this same topic for context makes the most sense to me.


#7

I suppose in this case it’s okay. For most topics you probably shouldn’t revive dead threads unless you have a related issue or something, but in cases like this it’s always a good idea to check.