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!
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 : []