0

I'm using python and I'm making a game, but there's one thing I don't get.

Why does this boolean reset? I don't want boolean 'powerup' to reset.

def spawnPowerup()
    number = 0
    powerup = False

    if (a certain thing happens, the thing happens for one frame.):
        number = random.randint(1,2)

    if number == 1 or powerup == True:
        print(powerup)
        powerup = True
        print(powerup)

The idea behind this code is, whenever the thing happens, I get a number. When the number is equal to 1, the powerup should be activated, but since the number in changing the whole time I need this boolean. So whenever the number is equal to 1 I change the boolean falue, so the if statement will always be true when the number has been equal to 1. But the problem is that the boolean resets. If the thing happens a couple of times this will be printed:

False
True
False
True
False
True
False
True
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What do you mean by the boolean resets? Can you put some more printlns to see whether it even resets when number == 2 ? – Anand S Kumar Jun 16 '15 at 14:33
  • On your second if statement there, the value of `powerup` will always be false because you change it to false at the beginning of the function – USFBS Jun 16 '15 at 14:36
  • What everyone is getting at here is that you need to look at the scope of the `powerup` variable. Right now because its inside a function, it's a private variable that gets reset every time. Consider putting `powerup` outside of `spawnPowerup` in a global scope. This will keep it from being reset each time you call `spawnPowerup` unless you reset it within the function itself. This is a good post to reference regarding scope http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – Kyrubas Jun 16 '15 at 14:44
  • @Kyrubas thank you! I understand it now! – Raymond Timmermans Jun 16 '15 at 14:49

3 Answers3

2

You always set the boolean to False when you enter the function, so naturally it is being reset. But this is a local variable; it doesn't exist outside the function anyway.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I don't know the exact English terms, but the problem will probably be fixed if I don't set powerup equal to False at the beginning of the function. I also don't want to set it to True, so I just want to do nothing with it, but letting the computer know it exists. How can I do that, or if I can't do that. What do you suggest I should do to fix the problem? – Raymond Timmermans Jun 16 '15 at 14:48
2

powerup is set to False every time this function is called. So when you're hitting:

if number == 1 or powerup == True:
        print(powerup)
        powerup = True
        print(powerup)

number must be 1 if you enter that block (unless you have other code that is not shown). So when you do your first print, powerup is still False. You set to True, then print again

sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • I don't know the exact English terms, but the problem will probably be fixed if I don't set powerup equal to False at the beginning of the function. I also don't want to set it to True, so I just want to do nothing with it, but letting the computer know it exists. How can I do that, or if I can't do that. What do you suggest I should do to fix the problem? – Raymond Timmermans Jun 16 '15 at 14:47
  • Sounds like you want a `global` variable. You can initialize it outside the function, let the function know it exists, and change it when necessary. This post should help: http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – sedavidw Jun 16 '15 at 14:56
1

Here's what's confusing you:

if number == 1 or powerup == True:
    print(powerup)
    powerup = True
    print(powerup)

It will only print the value of powerup here. At the first print statement, the value is always False, while afterwards, it is True. You will only print it out when the if block is executed, changing powerup to True. To explore more on this, just add a print(powerup) before the if block.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66