Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

A script which never stops (not an infinite loop)

Hi,

I am working on this python project in the techdegree and for some reason this script never stops running, now there aren't any infinite loops and i have tried in both the workspace console and VS code terminal but still won't work.

Could you help me please?

Much appreciated

import constants
import copy

if __name__ == "__main__":

    teams_main = copy.deepcopy(constants.TEAMS)
    players_main = copy.deepcopy(constants.PLAYERS)



    panthers = teams_main [0] = []
    bandits = teams_main [1] = []
    warriors = teams_main [2] = []


    def clean_data():

        for element in players_main:

            element['height'] = element['height'][0:2]
            element['height'] = int(element['height'])

            if element['experience'] == 'NO':
                element['experience'] = False

            elif element['experience'] == 'YES':                              
                element['experience'] = True

            element['guardians'] = element['guardians'].split(' and ')

    def balance_teams():

        panthers_exp = []
        panthers_inexp = []
        bandits_xp = []
        bandits_inxp = []
        warriors_xp = []
        warriors_inxp = []

        for player in players_main:

            while len(panthers) < 6:

                if player['experience'] == False and len(panthers_inexp) < 3:
                    panthers.append(player)
                    panthers_inexp.append(player['experience'])

                elif player['experience'] == True and len(panthers_exp) < 3:
                    panthers.append(player)
                    panthers_exp.append(player['experience'])


            while len(bandits) < 6:

                if player['experience'] == False and len(bandits_inxp) < 3:
                    bandits.append(player)
                    bandits_inxp.append(player['experience'])

                elif player['experience'] == True and len(bandits_xp) < 3:
                    bandits.append(player)
                    bandits_xp.append(player['experience'])             


            while len(warriors) < 6:

                if player['experience'] == False and len(warriors_inxp) < 3:
                    warriors.append(player)
                    warriors_inxp.append(player['experience'])

                elif player['experience'] == True and len(warriors_xp) < 3:
                    warriors.append(player)
                    warriors_xp.append(player['experience'])



    clean_data()
    balance_teams()

    for player in panthers:
        print(player)
        print()
    print(len(panthers))

1 Answer

Steven Parker
Steven Parker
231,269 Points

There are multiple "infinite loop" possibilities, depending on the contents of "constants" (not shown here). For example, assume the first "player" in "players_main" has a "False" value for "experience". Then this code is reached:

            while len(panthers) < 6:
                if player['experience'] == False and len(panthers_inexp) < 3:
                    panthers.append(player)
                    panthers_inexp.append(player['experience'])

The first 3 iterations of the "while" would append this player to both "panthers" and "panthers_inexp", but when the length of the latter reaches 3, the "if" will fail on all future iterations. But since the length of "panthers" never grows beyond 3, the "while" will remain stuck.

Hello Steven!

Thank you for your answer!

I tried earlier removing the While loop and it still won't stop

Hello Steven, It appeared that you were right, in my earlier removal attempt i forgot to remove the last while loop :D

Thank you alot for your help!!