Python Forum

Full Version: Team Chooser Challenge
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am new to python and am stuff with this assignment. i have created a team chooser code and, the challenge is to : Can you improve your program to split players into 3 teams instead of 2?
I have added another team but the syntax error state that the list is empty.
Please help!

#how to add in another team ?

from random import choice

players = []
file = open("players.txt","r")
# Read the list from the file and add to your PLAYERS list.
# The SPLITLINES code means that every line in the file is a new item in the PLAYERS list
players = file.read().splitlines()
print(players)

# create a list of team names from a file
team_names = []
file = open("team_names.txt", "r")
team_names = file.read().splitlines()
print(team_names)

#create empty team lists
teamA = []
teamB = []
# creatng another team 
teamC = []


# loop until there is no players left
while len(players) > 0 :
playerA = choice(players)
print (playerA)
teamA.append(playerA)
players.remove(playerA)
print("Players left : " , players)

playerB = choice(players)
print (playerB)
teamB.append(playerB)
players.remove(playerB)
print("Players left : " , players)
# new team 
playerC = choice(players)
print (playerC)
teamC.append(playerC)
players.remove(playerC)
print("Players left : " , players)

# you can tell your program to BREAK out of your WHILE loop if your PLAYERS list is empty
if players == [] :
break

# choose random teams names for the 2 teams
team_nameA = choice(team_names)
team_names.remove(team_nameA)
team_nameB = choice(team_names)
team_names.remove(team_nameB)
# new team
team_nameC = choice(team_names)
team_names.remove(team_nameC)

print ("Here are your teams : ")
print (team_nameA , ":" , teamA)
print (team_nameB , ":" , teamB)
print (team_nameC , ":" , teamC)
Error:
playerC = choice(players) File "/Users/travis/build/mu-editor/mu_portable_python_macos/python/lib/python3.6/random.py", line 258, in choice IndexError: Cannot choose from an empty sequence
Output:
['Harry', 'Hermione', 'Neville', 'Ginny', 'Freeman', 'Shelly', 'Isaac', 'jeanne'] ['Kuala Lumpur', 'Selangor', 'Johor', 'Kedah', 'Perak', 'Tenrengano', 'Penang', 'Ipoh'] Harry Players left : ['Hermione', 'Neville', 'Ginny', 'Freeman', 'Shelly', 'Isaac', 'jeanne'] Shelly Players left : ['Hermione', 'Neville', 'Ginny', 'Freeman', 'Isaac', 'jeanne'] Neville Players left : ['Hermione', 'Ginny', 'Freeman', 'Isaac', 'jeanne'] Isaac Players left : ['Hermione', 'Ginny', 'Freeman', 'jeanne'] jeanne Players left : ['Hermione', 'Ginny', 'Freeman'] Freeman Players left : ['Hermione', 'Ginny'] Ginny Players left : ['Hermione'] Hermione Players left : []
Please use proper code tags while posting your thread - see BBCode to know more
Please, fix the indentation of your code. As is, it's difficult to make sense of it.

Also, this is good example why you don't create variable names like you do, e..g. PlayerA, team_namesA,etc. What if you need to create 10, 50 or 100 teams/players. Would you have separate variable name for each player/team Use proper data structure - list/dicts/etc.
(Jun-01-2020, 05:09 AM)buran Wrote: [ -> ]Please, fix the indentation of your code. As is, it's difficult to make sense of it.

from random import choice
 
players = []
file = open("players.txt","r")
players = file.read().splitlines()
print(players)

team_names = []
file = open("team_names.txt", "r")
team_names = file.read().splitlines()
print(team_names)

teamA = []
teamB = []
teamC = []

while len(players) > 0 :
    playerA = choice(players)
    print (playerA)
    teamA.append(playerA)
    players.remove(playerA)
    print("Players left : " , players)
 
    playerB = choice(players)
    print (playerB)
    teamB.append(playerB)
    players.remove(playerB)
    print("Players left : " , players)

    playerC = choice(players)
    print (playerC)
    teamC.append(playerC)
    players.remove(playerC)
    print("Players left : " , players)

    if players == [] :
        break
 
team_nameA = choice(team_names)
team_names.remove(team_nameA)
team_nameB = choice(team_names)
team_names.remove(team_nameB)
team_nameC = choice(team_names)
team_names.remove(team_nameC)
 
print ("Here are your teams : ")
print (team_nameA , ":" , teamA)
print (team_nameB , ":" , teamB)
print (team_nameC , ":" , teamC)
Error:
Traceback (most recent call last): File "/Users/freeman/mu_code/random_players.py", line 39, in <module> playerC = choice(players) File "/Users/travis/build/mu-editor/mu_portable_python_macos/python/lib/python3.6/random.py", line 258, in choice IndexError: Cannot choose from an empty sequence