Need Help with a text based game | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need Help with a text based game

I'm trying to make a text based game where you create a character and then battle monsters the problem I'm having is the hit point variable resets to the amount set at the beginning of the game how do I get the variable to change and stay at the new value after taking damage

20th Oct 2017, 3:09 PM
David Aseltine
David Aseltine - avatar
15 Answers
+ 3
give this a read, it will fix your problem https://www.python-course.eu/global_vs_local_variables.php
20th Oct 2017, 7:49 PM
LordHill
LordHill - avatar
+ 2
you could use hp as a global variable. then it would change outside of the function.
20th Oct 2017, 7:48 PM
LordHill
LordHill - avatar
+ 1
No problem, just arrived. I'll check thru it in a few minutes and get back to ya
20th Oct 2017, 7:11 PM
LordHill
LordHill - avatar
+ 1
does this problem happen alot of mainly area 7? area 7 resets health when it starts
20th Oct 2017, 7:15 PM
LordHill
LordHill - avatar
+ 1
I believe the problem stems from the way you bring in your hp as an argument. inside of your battle function you have an argument of hp, then you battle and the hp changes, but when it is over and you leave the function and start reading the original hp, which has never changed, it results in the same hp from before. variables inside of functions are specific to that function.
20th Oct 2017, 7:39 PM
LordHill
LordHill - avatar
+ 1
''' Notice how the following code throws an error because hp inside the function doesnt exist. Same principle. The only reason your not getting the same error is because instead of the argument a, you have the argument hp. So when you call hp inside of your function it, does exist, and it has the value you passed it in with, which is your origional hp's value, but it's not really your hp, it is a new variable that works inside the function that happens to also be called hp ''' hp=100 def lower(a): hp=hp-5 print(a) lower(hp)
20th Oct 2017, 7:44 PM
LordHill
LordHill - avatar
+ 1
thank you so much for all your help I'll post again when I have the issue fixed or if I can't fix it still
20th Oct 2017, 8:04 PM
David Aseltine
David Aseltine - avatar
+ 1
well I got it work by getting rid of the battle function. now any of the areas that I want to have a battle have all the code for battling which means my code is very long but it works thank you again for all your help
21st Oct 2017, 1:18 PM
David Aseltine
David Aseltine - avatar
0
make sure you declare your variable before your game loop. health=100 while running: Your Game Logic this will have your health go down by 5 and stay there while running: health=100 Your Game Logic This will reset it to 100 everytime
20th Oct 2017, 4:20 PM
LordHill
LordHill - avatar
0
in the beginning of the game the variable is decided like this hp = random.randint(35,50) during a battle if damage is done hp = hp - damage however at the end of the battle function the game resets the hp variable to what ever random number was pick in the beginning so how do I make sure the damage received during the battle stays with the user till the end of the game do I need to make it a counter or do I need to re assign the hp variable at the end of battle
20th Oct 2017, 4:25 PM
David Aseltine
David Aseltine - avatar
0
it shouldn't do that unless it is being reassigned. going to need to see code to find the problem
20th Oct 2017, 6:38 PM
LordHill
LordHill - avatar
0
is there a way to upload my code to here I'm writing it on my laptop and have 496 lines of code would like not to have to re write all that
20th Oct 2017, 6:44 PM
David Aseltine
David Aseltine - avatar
0
ok email is sent thank you for taking the time to look at this for me
20th Oct 2017, 7:10 PM
David Aseltine
David Aseltine - avatar
0
if you go from area 1 to 3 so left when it ask the first time it resets there too I think any time there is a battle it resets
20th Oct 2017, 7:16 PM
David Aseltine
David Aseltine - avatar
0
ok so what would you suggest how can I have the ending hp variable in the battle function transfer to the rest of the program? or do I need to think of a new way to do the battle?
20th Oct 2017, 7:42 PM
David Aseltine
David Aseltine - avatar