[Tutor] Help with return results statement.

Alan Gauld alan.gauld at btinternet.com
Tue Oct 20 08:38:51 EDT 2015


On 20/10/15 12:29, Vusa Moyo wrote:
> Hi there. My script is as follows,

> alice = {
>      "name": "Alice",
>      "homework": [100.0, 92.0, 98.0, 100.0],
>      "quizzes": [82.0, 83.0, 91.0],
>      "tests": [89.0, 97.0]
> }

> # Add your function below!
> def average(numbers):
 >    total = sum(numbers)
 >    total = float(total)

That line isn't necessary since the inputs are floats already.

 >    total = total / len(numbers)
 >    return total
 >
> def get_average(student):
>      homework = average(student["homework"])
>      quizzes = average(student["quizzes"])
>      tests = average(student["tests"])
>      return 0.1 * homework + 0.3 * quizzes + 0.6 * tests
>
> def get_letter_grade(score):
>      if score >= 90:
>          return "A"
>      elif score >= 80:
>          return "B"

> print get_average(lloyd)
>
> def get_class_average(students):
>      results = []
>      for a in students:
>          b = int(get_average(a))
>          results.append([b])
>          return results
>

> get_class_average(alice)
>
> I receive a zero value for results, which doesnt quite make sense to me.

Nor to me. Are you sure its a zero result? It should be a list of some 
kind not a number. Or do you mean you get an empty list back?

Notice that get_class_average() expects your students value to be
some kind of sequence or collection. The for loop will iterate over 
that. If you pass Alice as a single student it will iterate over the 
keys, trying first of all to get the average of "Alice" which should 
fail with an error. Did you get any errors? If so please let us see them.

Please show us the actual code you execute, the actual output
and the full text of any errors.

One other thing that seems weird to me is that you go to great pains to 
produce a float as a result of get_average() but then you
immediately convert it to an int. Why not leave it as a float?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list