All Questions

Community

C
CramRengaw

Stuck on how to add all members using for loop

I don't know why I getting stuck but I think I'm having a beginner brain fart.

I can't seem to figure out how to add all members using a for loop in the "Challenge: Party members’ list".

I can't even, for the life of me, figure out which lesson to goto to remember how to cycle through a dictionary using a for loop and pass each entry in the dictionary to the add_member function.

What is throwing me off is that the argument that the add_member function is asking for is a dictionary and I don't know how to pass a dictionary to the argument.


  • Nathan Lovato replied

    This ise up we are lacki a real challenge because it combines things that we've done before but not directly in the last lesson.

    When you are stuck on a practice, please share the code that you have, even if it doesn't work. It will be much easier to help you with the bit that doesn't click yet without giving you the solution, so that you still get to practice and improve.

    Here are some hints to help you move in the right direction.

    The members constant is an array and each value in the array is a dictionary.

    So if you loop over the array with the for keywords, in each iteration of the loop, you're going to get one dictionary.

    for member in members:
        # do something with your member. The member temporary variable holds one of the array's dictionaries.

    When you use a for loop like that, on an array, you run a block of code once for each item in the array, regardless of what that item is. Here, it just happens to be a dictionary.

    I will let you work on this part and see if you can beat the challenge. If not, please share your code and I'll explain the next thing. If you have any questions they are also welcome, of course.

  • C
    CramRengaw replied

    Thank you for getting back to me!

    I finally made an opportunity to get back to this issue. Your explanation helped me in that I was finding it hard to wrap my mind around the member word in the line: 

    for member in members:
        # do something with your member. The member temporary variable holds one of the array's dictionaries.

    I couldn't track where the word "member" came from and so I didn't know its purpose or role in the line. If I'm understanding rightly, the word "member" in that line is a word that you make up which is a temporary variable that holds all the data of the first entry of the array (which in this case holds all the data of the first dictionary entry which would be the "name": "Dani"  and "texture": " (and the dani_neutral.png)

    Am I on the right track in understanding how that "member" word works in the for loop?

    p.s. I did get it to work by doing this...

    for member in members:
        add_member(member)
  • Nathan Lovato replied
    the word "member" in that line is a word that you make up which is a temporary variable that holds all the data of the first entry

    Exactly, it's a temporary variable that you can name however you want.

    It's not just the first entry in the array. The for loop will successively extract each dictionary from the members array and assign that value to the temporary variable named member.

    Let's change the names to make it clearer because you can name that however you want. The code below is technically the same code as yours, it works the same. I just changed the name for the values that get extracted one by one from the members array.

    for dictionary_value in members:
        add_member(dictionary_value)

    Hope this helps to clarify this a bit.