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

Chris Gravitt
Chris Gravitt
2,934 Points

Solution 4: Is this the clever way that was mentioned or will approaching a solution this way cause issues later?

Kenneth mentioned a clever way to complete this task when he was giving his solution. I get the correct answer using code this way, but I just want to make sure that I'm not getting myself caught up in a gacha.

for person in BIRTHDAYS:
    name = person[0]
    age = person[3]
    if person[2] and age <= 10:
        print(name, ' *'*(age))

For reference, here is the solution provided:

for person in BIRTHDAYS:
    name = person[0]
    age = person[-1]
    celebrates = person[-2]

   if celebrates and age <= 10:
        stars = ' '
        for star in range(age):
            stars += '*'
        print(name, stars)

1 Answer

Steven Parker
Steven Parker
231,007 Points

That seems quite safe, but that's probably only part of what Kenneth was thinking of.

He was likely imagining a list comprehension, perhaps like this:

for each in [p[0]+'  '+'*'*p[-1] for p in BIRTHDAYS if p[-2] and p[-1] < 10]:
    print(each)