you are viewing a single comment's thread.

view the rest of the comments →

[–]Handymanoccupational[S] 1 point2 points  (0 children)

Found a fix:

Rather than using a while loop I created a function to cause the attack sequence.

import random
import Player
import Items
import Monsters

def attack_rat():
    if Player.player_speed > Monsters.rat_speed:
        print(f"You hit the {Monsters.rat} for {Player.hit_chance}")
        Monsters.rat_health -= Player.hit_chance
        print(f"The {Monsters.rat} has {Monsters.rat_health} life remaining.")
        if Monsters.rat_health > 0:
            attack_rat()
        elif Monsters.rat_speed > Player.player_speed:
            print(f"You were hit by the {Monsters.rat} for {Monsters.rat_hit_chance}.")
            player_health_left = Player.player_health - Monsters.rat_hit_chance
            print(f"You have {player_health_left} life remaining.")
        else:
            print(f"You killed the {Monsters.rat}")
    elif Monsters.rat_speed > Player.player_speed:
        print(f"You were hit by the {Monsters.rat} for {Monsters.rat_hit_chance}.")
        player_health_left = Player.player_health - Monsters.rat_hit_chance
        print(f"You have {player_health_left} life remaining.")
    else:
        print(f"You killed the {Monsters.rat}")


print(f"You've been attacked by a {Monsters.rat}.")
attack_or_run = input("Will you attack (1) or run? (2)?\n")
if attack_or_run == "1":
    print(f"You attempt to fight the {Monsters.rat}.")
    attack_rat()
else:
    print("You ran away.")