It's Okay to be Average


Hello all mentors online,

I'm quite confused about "len(numbers)" on the forth line
May anyone explain more to me how this len() could count the number of scores I put in rather than the decimal of a number?

I remember for a string:
len() is counting it's characters, right?
then for a number like 2533
len("2533") = 4, right?
how could len(numbers) count the amount of scores in the dictionary?

thank you all :slight_smile: :blush: :sunny:

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


len() counts the number of items within a given object. So in "2533" it will count the total items in the string. When calling len on a list, it counts the items within the list.


Thank you so much :smiley:
Btw, as a just starting beginner, I'm curious that whether it's possible to count an integer?


Thank you again in advance :wink:


No, the program will throw an error because integers have no length, you could probably create a loop that does the count for you. For instance:

var1 = range(2533)
total = 0
for i in var1:
    i = 1
    total += i
print total

However, what would be the point? You already know that there are 2,533 items in a range of 2,533 items. You could modify the code slightly to add up all of the numbers within that range by removing the i = 1, which could be useful at times.

Here is a place you can go to test out code ideas that you may have without the constrictions of the what the lessons want you to do:

I believe you can also use Codecademy Labs, but I am on my Chromebook and it doesn’t seem to work on this so I can’t verify that.


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