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

Hara Gopal K
PLUS
Hara Gopal K
Courses Plus Student 10,027 Points

battleship project question

instead of print a grid with this code --> grid = [['O'] * 8 for number in range(8)], i wonder if this same can be done with a dictionary keys e.g{'A1' : 'O'.....}, it may become easier to manipulate the 'O' s and display the right symbol as the player chooses, but i was stuck with printing 'O' as grid from dictionary keys, any help here....

Hi Hara

I have just begun working on this project, and after alot of thinking, i too believe that using dictionaries would be the best way forward. I will come back to you once i have reached a point where i print the grid.

Steven Parker
Steven Parker
229,732 Points

Which course is this for? A link to it would be nice.

Steven Parker
Steven Parker
229,732 Points

A link would still be nice, but only other Techdegree students will be able to use it.

2 Answers

Steven Parker
Steven Parker
229,732 Points

I wouldn't expect to be able to use the "multiply" syntax for dictionary entries.

The reason I don't think that would work is because the keys all need to be unique.

Hi Hara

After a lot of heading banging, i ended up doing it using a 2D list, where each index in the outer list is a list in its self. Doing it this way you end up with a nice grid like system, where 'x' is the index on the outer list and 'y' this index on the inner array.

mylist = [[2,3,4],[5,6,7]]

mylist[0][1] # this will return 3

obviously you will need a bit more logic to work out the exact position of the grid.

Hope this helps you