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
#2
Please use proper code tags while posting your thread - see BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Meal cost challenge Emekadavid 3 2,855 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Appending To Files Challenge erfanakbari1 3 2,928 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 1,988 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 5,958 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