Python Forum
Python3.7 - Help with loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3.7 - Help with loops
#1
Hi...I'm a noob teaching myself python. I've got a simple turn based RPG program. Where I'm running into trouble is with updating the character hit points (warrior_hp). I'm trying to get the attack and damage functions to loop back and forth until one of the characters hit point counters hit zero...

 
class OrcBattle(Scene):

# Roll for initiative.
    def enter(self):
        print(dedent("Let's first roll for initiative..."))

        min = 1
        max = 20
        warrior_hp = 25
        orc_hp = 16

        initiative_roll = random.randint(min, max)

        if initiative_roll <= 10:
            print(dedent(f"""You rolled a {initiative_roll} and your initiative
                fails...The orc hears your footfall on some loose pebbles. He
                turns around and is now fully aware of you. He snarls and hefts
                an axe in both hands. You draw your sword and now it's time to roll for turns...
                """))

            OrcBattle.turns(self)

        else:
            print(dedent(f"""You rolled a {initiative_roll} which means that
                you have the element of surprise....You quietly draw your sword
                and swing at the back of the orc's head!
                """))

            OrcBattle.warrior_attack(self)

# If initiative fails, roll to take turns.
    def turns(self):

        min = 1
        max = 20

        turn_roll = random.randint(min, max)
        print(turn_roll)

        if turn_roll >= 10:
            print(dedent(f"You rolled a {turn_roll} therefor you go first!"))
            OrcBattle.warrior_attack(self)

        else:
            print(dedent(f"""You rolled a {turn_roll} therefor the orc gets the
                first attack.
                """))
            OrcBattle.orc_attack(self)

# Method for if you win the turn roll or win initiative.
    def warrior_attack(self):

        min = 1
        max = 20

        print("You swing your sword at the Orc!")

        turn_roll = random.randint(min, max)
        print(turn_roll)

        if turn_roll <= 10:
            print (dedent(f"""You rolled a {turn_roll}
                You missed! Now the Orc attacks...
                """))
            OrcBattle.orc_attack(self)

        else:
            print(f"You rolled a {turn_roll} that's a hit.")
            OrcBattle.damage_roll_warrior(self)

# Method for everytime it's the Orc's turn to attack.
    def orc_attack(self):

        min = 1
        max = 20

        print("The orc swings its ax")

        turn_roll = random.randint(min, max)
        print(turn_roll)

        if turn_roll <= 10:
            print(dedent(f"""The orc rolled a {turn_roll}.
                That's a miss! It's your turn."""))
            OrcBattle.warrior_attack(self)

        else:
            print(dedent(f"""The orc rolled a {turn_roll}!
                That's a hit. Lets roll for damage."""))
            OrcBattle.damage_roll_warrior(self)

    def damage_roll_warrior(self):

        warrior_hp = 25
        min = 1
        max = 20

        if warrior_hp >= 0:
            die_roll = random.randint(min, max)
            print(die_roll)

            damage = warrior_hp - die_roll
            warrior_hp = damage

            print(dedent(f"""
                You rolled {die_roll} points of damage. that leaves you with
                {warrior_hp} hit points left.
                """))
            OrcBattle.orc_attack(self)

        else:
            Print("You are dead!")
            return 'death'

    def damage_roll_orc(self):

        orc_hp = 16
        min = 1
        max = 20

        if orc_hp >= 1:
            die_roll = random.randint(min, max)
            print(die_roll)

            damage = orc_hp - die_roll
            orc_hp = damage

            print(dedent(f"""
                The orc rolled {die_roll} points of damage.
                That leaves the orc with {orc_hp} hit points left.
                """))
            OrcBattle.warrior_attack(self)

        else:
            print(dedent("""
                You have slain the orc! As it's black blood
                begins to pool under it's corpse you see that the only
                exit is the way you came.

                It looks like your only choice is not the South Tunnel.
                """))
            return 'south_tunnel'
I'm getting a "RecursionError: maximum recursion depth exceeded while calling a Python object". Because the hit point variables never update, the functions are essentially looping back and forth.
Where I'm falling down is how to store the variable, and continuously update it based on the damage rolls.
Reply
#2
You have a recursion problem. orc_attack calls damage_roll_warrior and damage_roll_warrior calls orc_attack which calls damage_roll_warrior and so on and so on and so on.

What you need is a third function that knows how to do a battle. It would take turns letting the warrior and orc attack until somebody dies. Battle would actually use a loop, a for or a while, instead of trying to do a loop with recursion which can make things complicated and isn't a good fit for this problem.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020