Difference in if statements inside functions vs outside

Please compare this two codes

a = 0

def build():
    if a == 0:
        hero.buildXY("fire-trap", 60, 60)
        a = 1
        
def collect():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    
while True:
    build()
    collect()

in this code above, my here doesn’t build any trap. He just collects coins.

a = 0
        
def collect():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    
while True:
    if a == 0:
        hero.buildXY("fire-trap", 60, 60)
        a = 1
    collect()

But if I change the previous one like this one just above, then the code works fine as I intended, which shouldn’t be different, I guess.

hmmmm…That is pretty weird. Probably a bug…

As I understand… In first case a will always be equal to 0. Let’s say function is like a book. When you take it from the shelf, it’s closed. If you read it till f.e. 55-th page and leave opened on the table, next time you take it you already got the page you need. But if you put book on the shelf, next time you take it, it will be closed again, and you’ll have to find 55-th page right from the start. So with a=0.
In while true loop functions are taken from the shelf closed, so

As Alexbrand said, it’s not actually a bug, you just can’t access outside variables within a function.
A way of fixing this (if you still want to use a function) is to use the global method:

outsideVariable = 0
def attack():
    enemy = hero.findNearestEnemy()
    global outsideVariable # what this is doing is calling the variable in from outside the function so you can use it.
    if enemy:
        hero.attack(enemy)
        outsideVariable = 1
while True:
    attack()

The enemy should attack once, then stop.
You can do the same for your function.
P.S. you cannot use the global method on the same line as a conditional statement e.g.

if global hello == "hello":
    print("good")

doesn’t work.
but:

global hello
if hello == "hello":
    print("good")

does.

How about redefining your def built to: def build(a):, which will require the call statement to be: build(a)

I’ve not tested this idea…where are you trying this, what level?

1 Like

Because global variables are not recommended two other solutions:


@brooksy125 , @Alexbrand - how this can be done with python closures?

1 Like

@xython
I wish I could say) I’m sorry, I don’t know :confused: I’m not a programmer and neither have IT or technical education, nor passed IT-courses like Coursera or smth like this. So I use only stuff I learnt here in codecombat levels and forum topics. In fact, I think, yours coding skills are much more better than mine.

Oh that’s right I was pretty stupid again :smiley: you gotta make it so that it looks like def build(a).