8. Part of the Whole : why Square brackets around the “i” in results.append(get_average([i])) needed ?!


My code works fine after many try an errors.
the key point that i added [] for the i in
results.append(get_average([i])) and without it , it gives many errors in different lines
I am still not convinced why it is needed to make it works !
check the 3 functions + successful output below

successful output


Guessing doesn't generally get you very far, instead we have to consider what is available and what we can do with that and what result we are looking for.

What are you looking to do in that location in the code?
What values are available to work with?
What can be done with those values?
How does doing those things look in code?

Since you already have some code that you're wondering what it does, you might consider what data type get_average is and what can be done with a value of that type.

Things don't just "snap into place", it's up to you to line everything up just right to produce the result you want!


I am using get_average which takes one dictionary as input ( such as
alice/tylor/ ) or a list of dictioneries ( each dictionary represents one
student records ) and return single value that is appended into the empty list
of results [].

My understanding is that the append operation should be written like this

  results.append( get_average(i) )

while "i" represents ( alice / tylor ... the student dictionary )
but it returns an error which is only fixed when I add ([i])

And inside



for i in students:

  print i["name"]   
       results.append(get_average( [i] ))

print results

return average(results)

print get_average(all_students)


That would mean that get_average would be testing the data type of the argument and taking different actions based on what it is, I don't think it does that, do you recall implementing something like that?

It would also be very strange to accept either one student, or a list of students. It would be much simpler to only accept a list of students, as that includes a list containing one student.

Whichever it is, make sure it matches what the instructions say that function should do!


is there's any link to compare my code with ?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.