FAQ: Learn Python - Exam Statistics - Computing the Average

This community-built FAQ covers the “Computing the Average” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Computing the Average:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

2 posts were split to a new topic: Why Do I Have to Use float()?

Hi guys,

lesson:
Exam Statistics / 5. Computing the Average

Please see my programm-code:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] # was given

def grades_sum(scores):
    result = 0
    for i in scores:
        result += i
    return result


def grades_averange(grades_input):
    factor1 = grades_sum(grades_input)
    result = factor1 / float(len(grades_input))
    return result

print(grades_averange(grades)) #call of fuction and call to print

Result in console right:
80.4230769231


Given Error:
grades_average seems to be missing!


Can someone give me a advice, what Im doing wrong?

you do not have a function named grades_average, which is why you get this error. Check your function name

On lines 12 and 13 of the solution, it says,
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))

I don’t see why I can’t replace this with:
average = grades_sum(grades) / float(len(grades_input))

This removes an extra line while giving the same answer.

less lines of code doesn’t necessary mean better code.

in this case, i don’t think it matters a lot

But isn’t it better to be concise?

sum_of_grades is an intermediate variable.

Sometimes an intermediate variable provides clarity concerning the functioning of your algorithm, sometimes it just seems to be clutter. I think it’s less a matter of conciseness than of aesthetics.

If, for instance, you’re going to need to compute many averages, “concise” code might result from defining a function to compute the average (or using numpy, where it’s a built-in), then making use of it whenever needed.

Hi, I’ve been playing around with this ‘computing the average’ code. I’ve written out two ways - one is a lot longer than the other!
I can’t figure out why Example 1 won’t print the correct result though? It prints 3. Examples 2 prints the correct number, 4.5. (Apologies the indents don’t show up but I don’t think that’s the problem!..)
Thanks for your help!
Alex

Example 1:

grades = [5, 4]

def grades_sum(x):
total = 0
for num in x:
total += num
total = total / len(x)
return float(total)
print grades_sum(grades)

prints 3

Example 2:

grades = [5, 4]

def grades_sum(x):
total = 0
for num in x:
total += num
return float(total)

def grades_received(x):
return float(len(x))

def avg(a, b):
total = grades_sum(grades) / grades_received(grades)
return float(total)

print avg(grades_sum(grades), grades_received(grades))

prints 4.5