Doubt about for iteration


#1

As instruções eram essas:

"Define a function called get_class_average that has one argument class_list. You can expect class_list to be a list containing your three students.

First, make an empty list called results.

For each student item in the class_list, calculate get_average(student) and then call results.append() with that result.

Finally, return the result of calling average() with results."

Eu coloquei isso:

def get_class_average(class_list):
results = []

for student in class_list:
  
   get_average(student)
    results.append(get_average(student))
  
  return average(results)

O que fiz errado?


#2
def get_class_average(class_list):
    #indent 4 spaces
    # create empty list
    results = []

    for student in class_list:
        # an extra 4 space indent
        #get_average(student) you do not need this
        results.append(get_average(student))
        # results will contain all the averages
    return results # for-loop is ended, now RETURN the =results= list

#3

Thank you so much!
that was awesome!


#4

I wrote that and he is saying:

“Make sure you defined get_class_average”

This is what i wrote:

def get_class_average(class_list):
results = []

  for student in class_list:
    results.append(get_average(student))
    return results

It makes no sense because i defined the functions get_class_average.


#5

Nevermind that problem. I was able to do it, but now he is telling me that my function only calculates the average of one student rather than the average of all students.

This is what i wrote:

def get_class_average(class_list):
results = []

  for student in class_list:
    results.append(get_average(student))
    return average(results)

This is the answer:
get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


#6

In Python the starting text position of a statement is VERY important !!

By way of =indentation-level= the Python Interpreter identifies the so-called =code-blocks=.

So for instance to express the FOR-loop code-block, this code-block will be executed as long as the =condition= of the code-block is true…

  for student in class_list:
    +-----------------------------------------------+
    | # 4-extra-spaces indent, START of CodeBlock   |
    | results.append(get_average(student)           |
    | # 4-extra-spaces indent, END of CodeBlock     |
    +-----------------------------------------------+
  # SAME 4-spaces LEVEL -of- FOR-loop statement=start is expressed,
  # so NEW-CodeBlock starts AFTER every-Student is processed in FOR-loop
  # RETURN statement will now execute AFTER all of FOR-loop is processed 
  return average(results)  # RETURN statement =return's= result
                           # AND causes EXIT of FUNCTION !!

now you write the code like

  for student in class_list:
    +-----------------------------------------------------------------------+
    | # 4-extra-spaces indent, START of CodeBlock                           |
    | results.append(get_average(student)                                   |
    | # SAME 4-spaces LEVEL -of- FOR-loop CodeBlock =start is expressed,    |
    | # so RETURN statement  for every-Student  in FOR-loop                 |
    | # RETURN statement will now execute                                   |
    | return average(results)  # RETURN statement =return's= result         |
    |                                       # AND causes EXIT of FUNCTION ! |
    | # 4-extra-spaces indent, END of CodeBlock                             |
    +-----------------------------------------------------------------------+

Thus as you process the 1st =student= you are executing the =return= statement
and thereby EXITing the get_class_average() FUNCTION


#7

I did it! and it worked! Thank you so much that was a genius tip!


#8

If you are looking for answers to your Python questions
you can google-search
== the Book ==
return statement site:python.org

or

to find
== dicussions /opinions ==
(Always start with the keyword python)
like
python why is identation important site:stackoverflow.com


#9

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