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 Flask Basics Character Builder Loop Nested Items

kirkbyo
kirkbyo
15,791 Points

Looping threw courses

I am having trouble with the second task of this code challenge.

Question

Now add a new <ul> inside of the <li> with a class of "courses". Inside this <ul> loop through the teacher's 'courses' key, creating an <li> for each course and printing the course.

My code

<ul class="teachers">
  {% for name in teachers %}
  <li>
    <h2>{{ name['name'] }}</h2>

    <ul class="courses">
      {% for name in teachers %}
      <li>{{ name['courses'] }}</li>
      {% endfor %}
    </ul>
  </li>
  {% endfor %}
</ul>

What am I doing wrong?

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi kirkbyo,

Currently you're looping over the teachers array again and are pulling the courses array without iterating over it, instead you need to iterate over courses and print out each course in their own perspective li element.

Also for clarity it's better to name your variables so they have meaning, so instead of just name I've changed the variable to teacher as common practise says to use the singular version of what you're iterating over instead of some random name.

<ul class="teachers">
  {% for teacher in teachers %}
  <li>
    <h2>{{ teacher['name'] }}</h2>

    <ul class="courses">
      {% for course in teacher['courses'] %}
      <li>{{ course }}</li>
      {% endfor %}
    </ul>
  </li>
  {% endfor %}
</ul>

Happy coding!

thanks =) I understand this much better

Ross Coe
Ross Coe
5,061 Points

forgot the <ul class="courses"> - read the instructions - doh!