I'm following an online Python project and having some issues. I don't feel like my code is very 'pythonic' and the code is not working correctly.
The program is supposed to read a CSV file of soccer players. The output is meant to split the players into three different soccer teams. Each team is supposed to have the same amount of players and the same amount with Soccer Experience. Out of the 18 players there are 9 with experience so that would equate to six team members with three of the six having experience. I can get it so each team has six players but the part i'm getting tripped up on is the experience part. Some of the teams are ending up with three but some are not. Here is what I've tried so far:
import csv
import random
def assign_players():
with open('soccer_players.csv') as csvfile:
soccerplayers = csv.DictReader(csvfile)
players = list(soccerplayers)
target = open('teams.txt', 'w')
raptors=[]
dragons=[]
sharks=[]
for player in players:
experienced_player = 0
if len(raptors)<6:
raptors.append(player)
if player['Soccer Experience'] == 'YES':
experienced_player+=1
if experienced_player >3:
break
elif len(dragons)<6:
dragons.append(player)
if player['Soccer Experience'] == 'YES':
experienced_player+=1
if experienced_player >3:
break
else:
sharks.append(player)
# if player['Soccer Experience'] == 'YES':
# experienced_player+=1
# if experienced_player >3:
# break
target.write("Raptors")
target.write("\n")
for raptor in raptors:
target.write(str(raptor["Name"])+ ', '),
target.write(str(raptor["Soccer Experience"])+ ', '), " ",
target.write(str(raptor["Guardian Name(s)"])+ ' '), " ",
target.write("\n")
target.write("\n")
target.write("Dragons")
target.write("\n")
for dragon in dragons:
target.write(str(dragon["Name"]) + ', '),
target.write(str(dragon["Soccer Experience"]) + ', '), " ",
target.write(str(dragon["Guardian Name(s)"]) + ' '), " ",
target.write("\n")
target.write("\n")
target.write("Sharks")
target.write("\n")
for shark in sharks:
target.write(str(shark["Name"]) + ', '),
target.write(str(shark["Soccer Experience"]) + ', '), " ",
target.write(str(shark["Guardian Name(s)"]) + ' '), " ",
target.write("\n")
if __name__ == "__main__":
assign_players()
This is a sample of how the soccer_players.csv file is formatted
Name,Height (inches),Soccer Experience,Guardian Name(s)
Joe Smith,42,YES,Jim and Jan Smith