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

List and Dictionaries

I was tasked to take a tasked with taking this list (Players):

https://github.com/josephyhu/Basketball-Team-Stats-Tool/blob/master/constants.py

And split it up into even teams, not only by quantity of players but also by a number of experienced players. So all teams had the same number of "non-experienced" players and "Experienced players". I was able to split the teams into equal teams pretty easily, but when it came to ensuring all teams had the same number of experienced players, It lost me. Below was my thought process, I thought I could copy the 'PLAYERS' list break that into an experienced list (exp_players) and non-experienced list (nexp_players), but it's backfiring on me. Even though this is no the most efficient way to do it, but I thought it should work. this is what I'm getting:(also taking advice on a more efficient way of doing this)

Traceback (most recent call last):                                                                                               
  File "/home/treehouse/workspace/123.py", line 25, in <module>                                                                  
    panthers = exp_panthers + nexp_panthers                                                                                      
NameError: name 'exp_panthers' is not defined Any guidance?
import random
from constants import PLAYERS
from constants import TEAMS

GREETING = 'BASKETBALL TEAM STATS TOOL\n'

players = PLAYERS.copy()
teams = TEAMS.copy()

print(GREETING.upper())


print('-----MENU-----\n')

panthers = exp_panthers + nexp_panthers
bandits = exp_bandits + nexp_bandits
warriors = exp_warriors + nexp_warriors

exp_players = []
nexp_players = []
max_eplayers = len(exp_players)/len(teams)
max_neplayers = len(nexp_players)/len(teams)


exp_panthers=[]
exp_bandits= []
exp_warriors=[]
nexp_panthers=[]
nexp_bandits= []
nexp_warriors=[]

def count_exp(players):
    for player in players:
        if player['experience'] == True:
            exp_players.append(player)
        else:
            nexp_players.append(player)

def balance_team_exp(exp_players):
    max_players = len(exp_players)/len(teams)
    for player in list:
        player_name = player['name']

        if len(panthers) < max_players:
            exp_panthers.append(player_name)
        elif len(bandits) < max_players:
            exp_bandits.append(player_name)
        elif len(warriors) < max_players:
            exp_warriors.append(player_name)

def balance_team_nexp(nexp_players):
    max_players = len(nexp_players)/len(teams)
    for player in list:
        player_name = player['name']

        if len(panthers) < max_players:
            nexp_panthers.append(player_name)
        elif len(bandits) < max_players:
            nexp_bandits.append(player_name)
        elif len(warriors) < max_players:
            nexp_warriors.append(player_name)                 

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The three lines under β€œmenu” banner seems out of place. Maybe they were to be placed lower, just above the function count_exp?

Post back if you need more help. Good luck!!!