• Create Account

## Noob Needs Help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Low_Man  Members

102
Like
0Likes
Like

Posted 15 July 2012 - 07:53 AM

Hello!

I'd like to prefice this by saying that I've only been learning Python 3 for a couple days now and really don't have a clue what I'm doing.

Anyway, the idea here is to have a turn based combat system. There's function that plays through to the end and then loops back to the beginning until someone has 0HP or less. The problem I'm having is that everytime it loops it resets the players health and the enemies health. I know why, it's because I declare the variable at the start of the function.

So what I'd like to know is how do I change the value of the variables "EnemyWHP" and "PlayerWHP"? So, after "EnemyWHP = EnemyWHP - Dice" how do I put whatever value that comes up with, into the "EnemyWHP" variable at the start of the program so that when it loops again, it shows the correct value?

Here's the relevant chunk of code:

Spoiler

### #2Servant of the Lord  Members

33494
Like
2Likes
Like

Posted 15 July 2012 - 09:11 AM

Since EnemyWHP and PlayerWHP are created in the function, they only last the lifetime of that function, and are recreated the next time the function is called (They are "local" to that function). You could make EnemyWHP and PlayerWHP "global" variables by defining them outside of the function, and then when you first use them in the function, declare the variables as global.

EnemyWHP = 20
PlayerWHP = 20

def Combat():
global EnemyWHP //Re-aquire 'EnemyWHP' (so we don't accidentally create a new one with the exact same name).
global PlayerWHP

EnemyWHP = EnemyWHP - 1


It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #3Low_Man  Members

102
Like
0Likes
Like

Posted 15 July 2012 - 11:51 AM

Awesome! Thanks, it works perfectly now.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.