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 Build a Soccer League

Justin Noor
Justin Noor
3,692 Points

Using a namedtuple() for the Soccer League Python project.

Hi how are you. Am I allowed to use a namedtuple() function for the Python tech degree project 1? What do you guys think about this? It seems apt because it allows you to access fields by names rather than index. I was unable to include the code because I could not start it like a regular workspace project.

It would look something like this:

from collections import namedtuple

Player = namedtuple('Player', ['first_name', 'height', 'experience', 'gaurdians'])

PreRoster = { Smith = Player([first_name='Joe', height='42 inches', experience='yes', gaurdians='Jim and Jan Smith']) }

6 Answers

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello Justin,

I had the same thought, I did not do it because of the statement that you should not use tools not covered up to the point the project is to be completed.

However, named tuple is in the collections library, and collections was the title of a course before the soccer league project, although it was not covered. I know for sure that named tuples was mentioned briefly - quote was something like if you have explored the collections library, this may be a good time to use named tuples here - in a course video, but on balance I decided not to use them

Hope that might help you in deciding what to do.

However, it does also say that if you identify a better way of doing things, that has not already been covered then you can add some comments in the code explaining what you would have done without restrictions.

I have a dilema for the battleships project where I have to use deepcopy from the copy library, as for all my efforts i could not find a good way to make a copy of a list with sublists in it that did not give me aliasing issues.

James

Justin Noor
Justin Noor
3,692 Points

Hey James. Thanks for the response.

It turns out iterating through named tuples is not so straight forward, and requires some steps that I'm pretty sure we did not cover. I'll just have to do it with a good old fashion dictionary hah.

Sorry about the dilemma on the battleship project. I am not familiar with the copy library. Did you ask the staff?

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

You are most welcome, for what it was worth.

It is a little difficult to remember what has been covered in the courses, when you have been reading around the course material - which I think is encouraged in the courses themselves - and especially if you have witched to the techdegree after progressing through the courses previously. I am sure the copy() method crops up somewhere in the python courses or workshops although I cannot remember where, the actual copy library has deepcopy() as the only extra thing I believe so its easy to master!

Have not asked the staff as yet, I will keep on thinking of how to get around the issue without using it if necessary, and can update the posting to github, but I think it may mean a complete restructuring of the code...

Jeff Hartl
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points

Hi James,

Did you ever solve your battleship issue? I've been trying to wrap my mind around that project and I'm struggling with nested lists for the board.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hi Jeff,

Still deepcopying...I think maybe the solution is to use a dictionary, with the keys being tuples of the row and column number and the values the symbol depending on hit/miss/sunk etc?

James

Jeff Hartl
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points

James, Funny you should mention dictionaries. I wrote one that maps user inputs to row-column tuples. For example the key "A1" would have the tuple (0, 0) as a value, and those values would refer to the index of an item within a nested list. All the way up to "J10": (9, 9). It hadn't occurred to me to include hit/miss symbols, etc.

Are you in the $199 (or higher) tier? You could get some advice on the Slack channel. Sorry I can't be of more help.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

No I am on the cheapo $99 version...

I think I will maybe use the dictionary idea, I like what you have done, I was thinking to have the key as a coordinate tuple as mentioned, and then store what that location is as the key as I had said mainly to make the screen printing easier. Set all values to 'empty' then update them as the game progresses with appropriate symbols depending on the state i want to represent. Then for printing the screen the symbol is already there to get and print out.