Python Forum

Full Version: While loop doesn't end when False
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm following a tutorial on how to code a very simple game. My code looks exactly like in the tutorial (https://youtu.be/kDdTgxv2Vv0?t=2667 for reference)

The code looks like this
player = {'name': 'Timmy', 'health': 100, 'attack': 75, 'heal': 16}
monster = {'name': 'Kanashi', 'health': 150, 'attack' :15}
game_running = True


while game_running == True:

        player_won = False
        monster_won = False

        print('Please select action')
        print('1) Attack')
        print('2) Heal')

        player_choice = input()

        if player_choice == '1':
            monster['health'] = monster['health'] - player['attack']
            if monster['health'] <= 0:
                player_won = True

            else: 
                player['health'] = player['health'] - monster['attack']
                if player['health'] <= 0:
                    monster_won = True

            print(monster['health'])
            print(player['health'])
        

        elif player_choice == '2':
            print('Heal Player')
        else:
            print('Invalid Input')

            if player_won == True or monster_won == True:
                game_running = False
My problem with this code is that the while statment continues to loop forever, after the while loop = false. What is wrong with my code?
The "game_running" is supposed to change from True to False after the Player or Monster reaches 0 or less health which should make the While loop, end the loop and therefor end the game. Simple enough? No, not for me. The loop doesn't end after the while loop has met the criteria for the loop to end.
Step through what is happening. You attack and take the monster to 0 or less hit points. That triggers the conditional on line 19, and player_won is set to True. That ends the if/else on lines 19-25, so it prints both player's health. That ends the if/elif/else chain on lines 17-37. That's the end of the loop, and game_running is still True. It doesn't get changed because the part that changes it is under the else statement on line 33. And when the loop starts over, player_won is reset.

What you need to do is unindent lines 36 and 37. That way they will run at the end of the loop no matter what the player's choice is. Remember: indentation is important. The wrong indentation will mean code doesn't run when you expect it to.

I would also suggest finding another tutorial. You shouldn't be testing while game_running == True:. It's better to test while game_running:. This is a more general test that checks if game running evaluates to True, rather than testing if it is equal to True. And really, you don't even need game running. The test on line 36 (which should be if player_won or monster_won: could be negated and used for the while loop:

player = {'name': 'Timmy', 'health': 100, 'attack': 75, 'heal': 16}
monster = {'name': 'Kanashi', 'health': 150, 'attack' :15}

player_won = False
monster_won = False


while not (player_won or monster_won):

    print('Please select action')
    print('1) Attack')
    print('2) Heal')

    player_choice = input()

    if player_choice == '1':
        monster['health'] = monster['health'] - player['attack']
        if monster['health'] <= 0:
            player_won = True

        else:
            player['health'] = player['health'] - monster['attack']
            if player['health'] <= 0:
                monster_won = True

        print(monster['health'])
        print(player['health'])


    elif player_choice == '2':
        print('Heal Player')
    else:
        print('Invalid Input')
Wow thank you for your detailed explanation. I'm going to continue following the tutorial and finnish the game, only for the reason that I want to learn from my mistakes and how I can make the code more efficent in the future. I might try reworking the code after I've finnished the tutorial, with my new experience, and follow your tips.

However, I have stumbled upon a syntax error:
    print(player['name'] + 's HP' + str(player['health'])

    while new_round == True:
This gives the following error:
    while new_round == True:
    ^
SyntaxError: invalid syntax
I know my string is causing this problem, but I don't understand what's wrong with it.

Thanks :)

edit: Nevermind, I found the reason. I forgot a ) to close the string. Thats interessting because I already tried that, but somehow it worked now, I don't know what I changed in the code in order for it to work this time

I have anoåther question however.
If I wanted to add a "Weapon" which increases the player["attack"], lets say by "10".
Would I be using upload( ) in order to give the player the weapon, and how would I write the code to make the "weapon" increase the player["attack"]?

Or would it juust be simpler to upload the weapon to the player, make it have a set damage of lets say 20 (how do I code this)

Add a new if statment that checks if the player has "weapon" (before the inputs)

If false, the program ignores this
If true, a new input will appear called, lets say "use weapon", which will subtract 20 health from the monster.