How come the return of the last line doesn't work, but print does?


#1


How come the return of the last line doesn't work, but print does? I used return at the end by accident and it returned an error message. I just wanted to know why. By the way you have to replace print with return in the code below to see the error

https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/3?curriculum_id=4f89dab3d788890003000096

The code works with print, but how come it does not work with 'return'.


lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    num=total/len(numbers)
    return num
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    sumofthree = .1*homework + .3*quizzes + .6*tests
    return sumofthree
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score>=80:
        return "B"
    elif score>=70:
        return "C"
    elif score>=60:
        return "D"
    else:
        return "F"
        
aaverage = get_average(lloyd)
letter_grade = get_letter_grade(aaverage)
print letter_grade


#2

This part is not a function, you only return things for functions. More info here:


#3

It is typical for learners to conclude that print and return are somehow related because they appear in roughly the same place in code patterns. We can see how that parallel would influence one's early judgement.

We soon find that return is in no way anything like print and has no parallel. return is a component of program flow; print is standard output, usually to the screen.

A function that has a simple purpose is easier to read and understand than one that tries to do too much. That's what this exercise illustrates. The average() function returns a float type arithmetic mean given a list of values. This is relatively obvious in the code pattern itself and requires no explanation. What is meant by, 'self documenting code'.

We can write a simpler function, but should be mindful of how the code is read. Can we still read this ...?

return float(sum(numbers)) / len(numbers)

Then this is a suitable way to write it. If not, then more self-description is needed. That always means more code. More weight is added to the program overhead.

We are given an example of the continuation operator, \ in the lesson text. The get_average() function is our opportunity to explore it. Again the function is very simple and requires no documentation.

def get_average(student):
    return average(student["homework"]) * 0.1 + \
    average(student["quizzes"]) * 0.3 + \
    average(student["tests"]) * 0.6

Notice that both of the above functions return a value. get_average() depends upon the return values of average() We can read this from the code.

There is much to glean from this exercise. Nothing to treat lightly, that's sure. return is a most important component. Any confusion around it needs to be remedied before moving forrward.


#4

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