0
#Knights and Fighters 

Knight_HP = int(input("Enter the Knight's HP: "))
Knight_Damage = int(input( "Enter the Knight's Damage Attack: "))
Knight_Special = int(input("Enter the Knight's Special Attack damage: "))
Fighter_HP = int(input("Enter the Fighter's HP: "))
Fighter_Damage = int(input( "Enter the Fighter's Damage Attack: "))
Fighter_Special = int(input("Enter the Fighter's Special Attack damage: "))
print (" ")
Round_N = 0

while Knight_HP and Fighter_HP > 0:
    print (" ")
    print ("Round: " + str(Round_N)) 
    for i in range(2):

        if i == 0:
            print ("The Knight goes first")
            Fighter_HP = Fighter_HP - Knight_Damage
            print ("Fighter: " + str(Fighter_HP))
            print ("Knight: " + str(Knight_HP))
            print ("")

        else:
            print ("The Fighter goes first")
            Knight_HP = Knight_HP - Fighter_Damage
            print ("Knight: " + str(Knight_HP))
            print ("Fighter: " + str(Fighter_HP))
            print (" ")
        Round_N = Round_N + 1
        if Knight_HP < Fighter_Special:
            print ("Fighter Has Used Special Attack.")
            print ("Knight Has Been Defeated.")
            print ("Round Over")
            break

        elif Fighter_HP < Knight_Special:
            print ("Knight Has Used Special Attack")
            print ("Fighter Has Been Defeated.")
            print ("Round Over")
            break

I want both loops to break when either's HP is 0 (i.e. when it uses the special attack), but for some reason, it continues making rounds, not breaking the loop when I need it to. The round number also skips some rounds (goes 0, 2, 4, 6, etc.), which is also bugging me out.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Does this answer your question? [Breaking out of nested loops](https://stackoverflow.com/questions/653509/breaking-out-of-nested-loops) – lucidbrot Mar 06 '20 at 13:12
  • 4
    I think you want `Knight_HP > 0 and Fighter_HP > 0`. Otherwise, the loop will continue if `Knight_HP` goes negative while the fighter still has hit points. – chepner Mar 06 '20 at 13:13
  • `while Knight_HP and Fighter_HP > 0:` should be what @chepner commented. Any integer different from 0 is truthy - so `if -4 and Fighter_HP>0:` evaluates to true and round you go unless Knight_HP gets exactly 0 – Patrick Artner Mar 06 '20 at 13:17

1 Answers1

-1

Put it in a function and use return this will end all other actions of the function.

e.g.

def update_round():
    while Knight_HP and Fighter_HP > 0:
        print (" ")
        print ("Round: " + str(Round_N)) 
        for i in range(2):

            #  other ifs

            if Knight_HP < Fighter_Special:
                print ("Fighter Has Used Special Attack.")
                print ("Knight Has Been Defeated.")
                print ("Round Over")
                return  # use return instead of break
Meshi
  • 470
  • 4
  • 16
  • 1
    still has the same problem as the OPs code - you do not test conditions correctly - putting it in a function and using return only masks bad logic. – Patrick Artner Mar 06 '20 at 13:18
  • The question is how to break out of nested loops. Using return is the correct answer. I've not looked to fix other bits in the code, just showing an example that will solve his question – Meshi Mar 06 '20 at 13:19