Don't know what's wrong with my code - 5/9



My code:

def average(numbers):
[indent] total = sum(numbers)
[indent] float(total) / len(numbers)
[indent] return average(numbers)

I searched this exercise in these forums and saw that someone else had about forty lines of code for this problem. One of the mods recommended using a for-loop to make it more elegant, but… the instructions don’t tell me to do anything like that. Am I supposed to incorporate those dictionaries above my code somehow into this actual code? It seems like the object here is to define a function to find averages, not to actually find the averages.


Hey there.

Student Becomes The Teacher is a pretty tricky lesson, as it’s intended to make you think about what you’ve learned up to that point and apply it to a problem with less prompting from Codecademy than you’ve had previously.

I note you’ve not mentioned whether you’re getting an error, other than the red checkbox, or what it is if you are, but if we look at your code…

def average(numbers):
  total = sum(numbers)
  float(total) / len(numbers)
  return average(numbers)

…we can see whether you’ve met the requests that the exercise is making.

  • Define a function called average that has one argument, numbers: Done.
  • Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total: Check.
  • … use float() to convert total and store the result in total: You’re being a bit DRY and combining this and the next step…
  • Divide total by the length of the numbers list. Use the built-in len() function to calculate that: Done on line 3, but where is this new value going??
  • Return that result:. If we look closely at your function’s return statement, what exactly is it you’re returning?

I’d say you’re a good 80% of the way to cracking this one, there’s just a few little things missing from your code and you’ve nailed it. :slight_smile:


Thank you for this, I’ll dig in and see if I can’t figure those parts out.

I don’t actually understand what return does, I only know enough to write return function_name at the end of my functions when the instructions tell me to do it. I think it actually runs the function? Or prints something?

Aside: How do you get text to indent in this editor? I always have to write “[indent]” when I’m pasting my code into this text editor.


Oh and the error I’m getting is at the bottom of the editor:

average([3, 0]) resulted in an error: maximum recursion depth exceeded while calling a Python object

Edit: When I set total equal to float(total) I get this error:

File “python”, line 24
SyntaxError: can’t assign to function call


I made a mistake in my previous post, which I’ve corrected. Hopefully it’ll be a bit clearer where the error is.

If you want to paste in code and preserve the formatting, you can put ``` before and after the code and it’ll render like this:

code stuff
    i have remembered my indent
  im also indented

I think you’re using the blockquote > character. ( ` is the key above tab, on my keyboard. Probably the same on most others, I imagine. Maybe not on a laptop?)

On the subject of return

A function is essentially a reusable block of code that performs a specific job. In our case here, the function average takes some input in the form of numbers, does some stuff with that input, and then provides you with a result - the average of the numbers you gave it.

return is simply the mechanism by which you’re telling Python to go back to wherever the function was called from, with a result (or output) if that’s appropriate.

To put it in terms of a real-world analogy, let’s imagine that you have a colleague at work who is turning 40 and you and the team want to get them a really nice cake from an amazing bakery near the office.

In this analogy, you’re giving the bakery money and your requirements and after a bit of work they give you a cake. We could say…

budget = 60
my_requirements = {
  "dietary" : "No peanuts",
  "design" : "Philadelphia Eagles themed"

def bake_me_a_cake(cash, requirements):
  delicious bakery magic here
  return cake

my_cake = bake_me_a_cake(budget, my_requirements)

and so your cake (that is, the variable my_cake) comes out of the bakery by means of the return statement.

Does that help? :slight_smile:

numbers = [1,2,3]
def average(numbers):
    total = sum(numbers)
    av = float(total) / len(numbers)
    return av

This code will give you the average of a list of numbers. av = float(total) / len(numbers) gives you a single number (the average).
When we return av we get that number.

We can display that number by calling print average(numbers)
We can also use that number in other ways for example

ave_plus1 = average(numbers) + 1
print ave_plus1

Try to understand how return works
Here is an example that may make it more clear.

def return_3():
    a = 1
    b = 2
    c = 3
    return c

print return_3()   #outputs 3
d = return_3() + 1
print d #outputs 4