Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Soccer League: Return something writable from a function for output into text file?

I've gotten my three more or less equal teams into three lists of lists, and have figured out how to print each list out as the project guidelines require, but I'm stuck on how to bridge that to something the .write() method can work with for the output file. I can make a function printing it out properly, but don't know how to get that function to return something .write() can use. I'm getting kinda tired of laboring over this and would like to see if anyone else can help me out. Here is my code below; where I'm stuck starts right at def printing(a_list). Thanks!

import csv
import random

'''
LeagueBuilder, by Adam Cameron, 2017
Take 18 youth soccer players and divide them into three more or less equal teams.

Comments explain the lines of code immediately following.

'''

# Put all code inside this block at the end!
#if __name__ == "__main__":

# Declare three empty lists corresponding to team titles.

sharks = []
dragons = []
raptors = []

# Declare two empty lists corresponding to experience/inexperience.

experienced = []
inexperienced = []

# Open CSV file and instantiate csv.reader object

with open('soccer_players.csv') as csv_file:
    player_reader = csv.reader(csv_file, delimiter=',')

    # Skip headers
    next(player_reader, None)

    # Append experienced and inexperienced lists with data from CSV file.
    for line in player_reader:
        if line[2] == 'YES':
            experienced.append(line)
        else:
            inexperienced.append(line)

    # Shuffle lists for sake of randomness.
    exp_shuffled = random.shuffle(experienced)
    inexp_shuffled = random.shuffle(inexperienced)

    # Allocate experienced team members to three empty team lists.
    # Length of 'experienced' list is divided by three, after which
    # the team list variables are reset to include list slices based on list length.

    members_per_team = len(experienced) // 3
    sharks = experienced[0: members_per_team] # E.g.: slice of 'experienced' from beginning to the length of the list divided by 3.
    raptors = experienced[members_per_team: 2 * members_per_team]
    dragons = experienced[2 * members_per_team:len(experienced)]

    # Allocate inexperienced team members to lists by declaring new lists and concatenating them with lists
    # already containing experienced players.

    final_drags = dragons + inexperienced[0:members_per_team]
    final_sharks = sharks + inexperienced[members_per_team:2 * members_per_team]
    final_rapts = raptors + inexperienced[2 * members_per_team:len(inexperienced)]



def printing(a_list):
    for i in a_list:
        a = ', '.join(i)
    return a

with open('teams.txt', 'w') as f:
    f.write(str(printing(final_drags)))

1 Answer

Okay, I'm sure there's a less awkward way of doing it but I seem to have more or less accidentally figured it out. You can just make an empty string variable and then concatenate it with new output until you get what you want. Here's how I did it in a function:

def joining(the_list):
    empty = ''
    for i in the_list:
        empty = (empty + ', '.join(i)) + '\n'
    return empty

This gives you a string with each list item joined with a comma and a space, and on a separate line to boot! And what's better, it only took me about five hours today to figure this out! Evidently, from here you can then .write() to the .txt file with joining(team_list) as the argument.