0

I was 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. But there has to be a simpler way to do this and I just can't see it. Here's the error I'm getting for my current code:

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
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):
    for player in exp_players:
        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):
    for player in nexp_players:
        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)  

My expected output would be the three teams (panthers, bandits and warriors) would have equal amount of players and equal players that experience == True.

  • 1
    you have assign this variable ```panthers = exp_panthers + nexp_panthers``` before the ```exp_panthers``` is assigned. – bilakos Jan 24 '21 at 00:03
  • so do you need to split them up into 3 teams? As long as you have a list of experinced, and a list of not experienced, you can pull a random player form the list (without replacement) into 3 lists – chitown88 Jan 24 '21 at 11:12

2 Answers2

0

As i see it you will have to move those variables:

exp_panthers=[]
exp_bandits= []
exp_warriors=[]

to this line of code:

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

By doing this i think that you will not get the error name .... is not defined

bilakos
  • 145
  • 7
  • Yea, I've been moving the variables around. When I do that, the program runs but then I get this: BASKETBALL TEAMS STATS TOOL ------Menu--------- [] [] [] Am I even on the right track the way I have it set up? It seems like there is a simpler way to do this. – the_buthcher1 Jan 24 '21 at 00:21
  • thats happens because the ```lists``` from your ```variables``` are empty. Look at your code. You are telling the ```panthers``` variable to my a calculation with zero entrys. – bilakos Jan 24 '21 at 00:24
0

You imported random, but I don't see it being used. But yes, that's how I was thinking about it.

First thing you need is to seperate the experienced players from non-experienced. Then you could use random to randomly select a player (without replacement) from each list (exp and nexp) and place them into a team individually 1 by 1.

Or use random to partition those 2 lists into n number of teams. I chose this way.

So first split the list of players into a experienced list and not experienced list. Shuffle each of those lists so its random. Then partitioned each list into a list of 3 (because there are 3 teams). 1st group goes into team 1, 2nd group into team 2, and 3rd group into team 3, etc. And you just do that for the experienced and non-experienced lists.

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')


# Split players into exp and nexp
def split_into_exp_nexp_players(players_list):
    exp_players = []
    nexp_players = []
    for player in players_list:
        if player['experience'] == 'YES':
            exp_players.append(player)
        else:
            nexp_players.append(player)
            
    return exp_players, nexp_players


# Function that will split a list into equal n lists
# credit: https://stackoverflow.com/questions/3352737/how-to-randomly-partition-a-list-into-n-nearly-equal-parts
def partition (list_in, n):
    random.shuffle(list_in)
    return [list_in[i::n] for i in range(n)]


# Using that function above, create the equal splits into n teams
def split_into_teams(teams, exp_players, nexp_players):
    exp_split = partition(exp_players, len(teams))
    nexp_split = partition(nexp_players, len(teams))
        
    return exp_split, nexp_split


exp_players, nexp_players = split_into_exp_nexp_players(players)
exp_players, nexp_players = split_into_teams(teams, exp_players, nexp_players)


# Put each of those partitioned players into teams
team_rosters = {}
for idx, team in enumerate(teams):
    comb_players = exp_players[idx] + nexp_players[idx]
    team_rosters[team] = comb_players
    

for team, roster in team_rosters.items():
    print('%s:' %team)
    for each_player in roster:
        print(each_player['name'], each_player['experience'])
        
    print('\n')

Output:

Panthers:
Bill Bon YES
Herschel Krustofski YES
Diego Soto YES
Kimmy Stein NO
Sammy Adams NO
Eva Gordon NO


Bandits:
Jill Tanner YES
Phillip Helm YES
Suzane Greenberg YES
Ben Finkelstein NO
Matt Gill NO
Sal Dali NO


Warriors:
Karl Saygan YES
Joe Smith YES
Les Clay YES
Arnold Willis NO
Joe Kavalier NO
Chloe Alaska NO

You then have preserved any data wanted for the players in json format:

print(team_rosters)
{'Panthers': [{'name': 'Bill Bon', 'guardians': 'Sara Bon and Jenny Bon', 'experience': 'YES', 'height': '43 inches'}, {'name': 'Herschel Krustofski', 'guardians': 'Hyman Krustofski and Rachel Krustofski', 'experience': 'YES', 'height': '45 inches'}, {'name': 'Diego Soto', 'guardians': 'Robin Soto and Sarika Soto', 'experience': 'YES', 'height': '41 inches'}, {'name': 'Kimmy Stein', 'guardians': 'Bill Stein and Hillary Stein', 'experience': 'NO', 'height': '41 inches'}, {'name': 'Sammy Adams', 'guardians': 'Jeff Adams and Gary Adams', 'experience': 'NO', 'height': '45 inches'}, {'name': 'Eva Gordon', 'guardians': 'Wendy Martin and Mike Gordon', 'experience': 'NO', 'height': '45 inches'}], 'Bandits': [{'name': 'Jill Tanner', 'guardians': 'Mark Tanner', 'experience': 'YES', 'height': '36 inches'}, {'name': 'Phillip Helm', 'guardians': 'Thomas Helm and Eva Jones', 'experience': 'YES', 'height': '44 inches'}, {'name': 'Suzane Greenberg', 'guardians': 'Henrietta Dumas', 'experience': 'YES', 'height': '44 inches'}, {'name': 'Ben Finkelstein', 'guardians': 'Aaron Lanning and Jill Finkelstein', 'experience': 'NO', 'height': '44 inches'}, {'name': 'Matt Gill', 'guardians': 'Charles Gill and Sylvia Gill', 'experience': 'NO', 'height': '40 inches'}, {'name': 'Sal Dali', 'guardians': 'Gala Dali', 'experience': 'NO', 'height': '41 inches'}], 'Warriors': [{'name': 'Karl Saygan', 'guardians': 'Heather Bledsoe', 'experience': 'YES', 'height': '42 inches'}, {'name': 'Joe Smith', 'guardians': 'Jim Smith and Jan Smith', 'experience': 'YES', 'height': '42 inches'}, {'name': 'Les Clay', 'guardians': 'Wynonna Brown', 'experience': 'YES', 'height': '42 inches'}, {'name': 'Arnold Willis', 'guardians': 'Claire Willis', 'experience': 'NO', 'height': '43 inches'}, {'name': 'Joe Kavalier', 'guardians': 'Sam Kavalier and Elaine Kavalier', 'experience': 'NO', 'height': '39 inches'}, {'name': 'Chloe Alaska', 'guardians': 'David Alaska and Jamie Alaska', 'experience': 'NO', 'height': '47 inches'}]}
chitown88
  • 27,527
  • 4
  • 30
  • 59