Python Forum
Team Chooser Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Team Chooser Challenge
#1
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 : []
Reply


Messages In This Thread
Team Chooser Challenge - by kanchiongspider - Jun-01-2020, 03:31 AM
RE: Team Chooser Challenge - by pyzyx3qwerty - Jun-01-2020, 04:24 AM
RE: Team Chooser Challenge - by buran - Jun-01-2020, 05:09 AM
RE: Team Chooser Challenge - by kanchiongspider - Jun-02-2020, 04:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Meal cost challenge Emekadavid 3 2,886 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Appending To Files Challenge erfanakbari1 3 2,966 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 2,013 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 5,993 Jun-21-2017, 07:07 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020