Python Forum
While loop doesn't end when False
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop doesn't end when False
#1
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.
Reply
#2
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')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,930 Dec-18-2021, 02:38 AM
Last Post: knight2000
  While loop doesn't exit KenHorse 3 1,967 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  While loop = False NectDz 1 1,724 Jun-03-2020, 04:35 PM
Last Post: GOTO10
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,186 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  For loop in my __init__ doesn't work as expected Jessy 2 2,318 Nov-18-2019, 10:07 AM
Last Post: buran
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,895 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,153 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Returning true or false in a for loop bbop1232012 3 8,036 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Get True of false outside a loop morgandebray 2 2,415 Aug-09-2018, 12:39 PM
Last Post: morgandebray
  Code doesn't loop after first RFID scan BadgerHack 5 4,227 May-01-2018, 11:07 AM
Last Post: BadgerHack

Forum Jump:

User Panel Messages

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