Using While loop and updatinf a variable

I have a working function that I plan to use in a text based game. The player meets and enemy, they fight and one of them wins. No issues with this part. However, the functions runs once and I want the fight to last three rounds. If the player wins their new health level is stored. This is the part I am unsure how to complete.

This is code:

Hero__Health = 100

Enemy_Attack_Points = random.randint(10, 80)

# functions

def Fight_Rat_King():

    global Hero__Health

    global Enemy_Attack_Points

    Hero__Health -= Enemy_Attack_Points

    if Hero__Health < 1:

        print("You are defeated")

    if Hero__Health > 1:

        print("You win. Your health is {}%".format(Hero__Health))

Fight_Rat_King()

As a beginner, please be nice to me. Any help with the while loop, and the update of Hero_Health will be appreciated.

What have you tried so far? What do you think might work here? You mentioned while loop, how would you use it?

1 Like

While you should avoid global variables…
What do you mean “their new health level is stored”? You literally store the health outside of the function anyway.
On a sidenote, you also generate the attack points outside, so they will have the same value every time. If you want 3 battles, the moment the randint is above 33, the hero will lose - which has a chance of almost 70%.

And for 3 fightes - you need to somehow count to 3 as long as health is above 0.

1 Like

Thank for the advice, which I used to redo my function. It works they way I expected . This is what I came up with:

def Fight_Rat_King():

    New_Health = 0

    Hero__Health = 100

    Enemy_Attack_Points = random.randint(10, 20)

    print("You health is {}%.".format(Hero__Health))

    time.slep(2))

    Count = 3

    while  Count > 0:

        Hero__Health -= Enemy_Attack_Points

        Count -= 1

        if Hero__Health < 1:

            print("You are defeated")

        if Hero__Health > 1:

            print("You stuck the ememy hard, but the emeny hit back. Your health is {}%".format(Hero__Health))

            time.sleep(2)

    New_Health = Hero__Health

    print("Your health is {}%.".format(New_Health))

Fight_Rat_King()

You can see that New_Health = Hero_Health. As this function will not be used further in the game, whether the enemy dies or not, I still need the New_Health for other fights along the way. What would be the best way to do this?

Thanks.

Hm… well the “best” way would be to implement object-oriented programming and turn the hero into an object with a dedicated health-stat.

Ofcourse if you just start out: You can have the hero-health as global value BUT instead of changing it within the function, you provide it as argument to the function and return the new health.

Reason is, that when programs get more complicated and code might be spread out over several files and functions, maybe even other people working on part - nobody has the entire code at hand. So especially the “global” keyword can make it hard to actually figure out where changes come from and what they affect.

Whereas if you write hero_health = Fight_Rat_King(hero_health) without any global variables you know exactly which values are getting changed (only hero_health) and ONLY those are getting changed.

1 Like

Thank for the help. Problem now solved.