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 trialFlore W
4,744 PointsIn the Dungeon Game, Kenneth mentions that CELLS could be a tuple instead of a list of tuples. What would it look like?
Does he mean it would be something like (x, y) with x and y being variables?
3 Answers
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsYes, you can have a tuple of tuples as you have highlighted above. I am no expert in Python but I dont this gains you much over a list of tuples.
One thing maybe of benefit is that Tuples are immutable (cant be changed) therefore given that we have fixed set of cells, using a Tuple means that we could not inadevertenly change something., Also tuples are meant to perform better than lists....
Iulia Maria Lungu
17,546 PointsI tried myself to use tuple instead of list. It actually makes sense because that should be a immutable. We don't want to change the CELLS.
It's supposed to be constant.
I've seen random lib sample
works with tuples as well.
from the docs:
random.sample(population, k)
for instance if you feed a dictionary to the random.sample
you get an error:
Population must be a sequence or set
So your
CELLS = ((0,0),(0,1),(0,2),
(1,0),(1,1),(1,2),
(2,0),(2,1),(2.2))
makes total sense, just that the final cell has a float
in it instead of a tuple
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi there, yes the cells could look like the following
CELLS = [(0,0),(0,1),(0,2),
(1,0),(1,1),(1,2),
(2,0),(2,1),(2.2)]
Flore W
4,744 PointsHey Stuart, that's a list of tuples, I was more wondering what it would look like as a tuple since Kenneth mentioned it. Could CELLS look like:
CELLS = ((0,0),(0,1),(0,2),
(1,0),(1,1),(1,2),
(2,0),(2,1),(2.2))
and what would be the difference?