user9730036
user9730036

Reputation:

Trying to compute the average in python

So i'm a newbie with python and i'm struggling with the new syntax. I'm trying to create a script that just assigns an age to the amount of people i call in the function paramter, prints out each age, and then computes the average age.

This is the script.

import random as rnd

def age_func(size):

    listofAges = []
    totalSum = 0
    average = 0

    for i in range(size):
        ages = rnd.randint(0, 100)
        listofAges.append(ages)
        totalSum += ages
        print('Age is', ages)
    average = totalSum / size
    print('Average age', average)
    print('totalSum', totalSum)
    return listofAges, totalSum, average     
val1, val2, val3 = age_func(6)
print (val2)

So the problem is with the totalAges, it's not adding each age as the loop increments and therefore the final average is just the last age divided by the total size. Or i think, i may be completely doing this in a bad way.

Upvotes: 0

Views: 734

Answers (2)

Danielle M.
Danielle M.

Reputation: 3662

You have your answer from Ralf, but I wanted to point out another little bug you might have in your code. I modified your code a little to show it, and also used a slightly more concise way of calculating sums and averages of lists:

import random


def age_func(size):

    age_list = list()

    for _ in range(size):
        age = random.randint(0, 100)
        age_list.append(age)
        print('Age is {}'.format(age))

    average_int = sum(age_list) / len(age_list)
    average_float = float(sum(age_list)) / float(len(age_list))

    print('Average age (int): {}'.format(average_int))
    print('Average age (float): {}'.format(average_float))
    print('total Sum: {}'.format(sum(age_list)))
    return age_list

my_list = age_func(6)
print (sum(my_list))

A few things I changed:

  • for _ in range(size) - using _ as the variable name means something like "I don't actually use this variable for anything"

  • imported random as random - ensures anyone going through your code realises this is the default random module, not some custom module.

  • use format strings everywhere - format strings are the best :)

  • use sum(age_list) and len(age_list) once your loop is done. No need to calculate these at every step in the loop, only needed once.

  • if you ask python to divide an integer by another integer, it will return an integer. Often, that's not what you want, as in this case. Try asking python to do evaluate 7 / 4 and see what happens :) By forcing our integers to become floats, the results will also be a float. Retry the previous calculation as 7.0 / 4.0.

  • changed variable names to snake_case

Upvotes: 2

ralf htp
ralf htp

Reputation: 9422

the following is working :

import random as rnd

def age_func(size):

    listofAges = []
    totalSum = 0
    average = 0

    for i in range(size):
        ages = rnd.randint(0,100)
        listofAges.append(ages)
        totalSum += ages
        print('Age is', ages)
        print('totalSum is', totalSum)
    average = totalSum/size
    print('Average age',average)
    print('totalSum',totalSum)
    return listofAges, totalSum, average     
val1, val2, val3 = age_func(6)
print (val2)

alternatively you can use numpy ( numpy.random.randint and numpy.mean https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.randint.html , https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html )

Upvotes: 1

Related Questions