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

Jeffery Baker
Jeffery Baker
2,013 Points

Trying understand the pieces of my code (go figure)

class Game:
    def __init__(self):
        self.size = 4
        self.card_options = ['Add', 'Boo', 'Cat', 'Dev',
                             'Egg', 'Far', 'Gum', 'Hut']
        self.columns = ['A', 'B', 'C', 'D']
        self.cards = []
        self.locations = []
        for column in self.columns:
            for num in range(1, self.size + 1):
                self.locations.append(f'{column}{num}')

Above you will find the Python code for creating the card game, and my question has to do with the last line here. In laymen's terms, what is the point in using the append function for this situation? Just trying to understand what this loop is doing. Thank you in advance!

1 Answer

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Hi Jeffrey! In each instance of your game, self.locations is a list that holds locations for your cards (like A1, B2, C3, D4, etc.)

That loop is working like this:

for column in self.columns:
# for A in self.columns
            for num in range(1, self.size + 1):
            # for num in range (1-5)
                self.locations.append(f'{column}{num}')
                #self.locations.append(f'{A}{1}')

So, "A1" will be added (appended) to self.locations. In the next iteration of the loop, A2 will be added. Then A3 and A4. This will occur for B1-B4, C1-C4, and D1-D4 as well.

When you create the set_cards and check_location methods later, you will refer back to the self.locations list.

Does this answer your question?

PS- Next time you post a question on the forums, try and associate it with the video or challenge you encountered your problem on.

If you click "Get Help" on the video or challenge page, you'll go through the process of writing your forum post. When you post it, the video or challenge will be attached to your question. It will help others answer your question.