Display name
Display name

Reputation: 205

Initialize a list and find the mean of that list

My first function takes a list(L) of numbers and finds the average, it works perfectly.

def mean(L):
    x=sum(L)
    print x/float(len(L))

In my second function I am trying to ask the user to input 10 elements, initialize them into a list, and then use the first function mean(L) to find the average of the list:

def Values():
    L=[raw_input('enter element ') for i in range (0,10)]
    L=L[0::1]
    print L[0::1]
    print mean(L)

but i keep getting this error message:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Upvotes: 2

Views: 70

Answers (1)

falsetru
falsetru

Reputation: 369054

raw_input returns a string. Convert it to numeric type like int, float, ...:

L = [float(raw_input('enter element ')) for i in range (3)]

The function mean currently print the result. It's better to make it return the result so that print mean(L) will print the result instead of None.

Upvotes: 3

Related Questions