seyazc
seyazc

Reputation: 19

calculating the average of elements in a tuple python

a=('A','1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1030')
b=('B', '-1.0', '1', '-2.443', '-1.456', '4.00', '3.00', '-2.343', '2.112', '3.00', '')

I want to exclude first element Ave B from a and b and calculate the averages of the values in a and b tuple. I was trying to write a code like this:

def avarage(value):
    sum=0
    value=list(value)
    first=value[0]
    value.pop(0)
    for i in value:
        sum+=i
    avrg=sum / (len(value))
    return avrg

print(avarage(a))
print(avarage(b))

I got a few mistakes like this and this:

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

How can I resolve this error or how can I find the average?

Upvotes: 0

Views: 7688

Answers (5)

tonypdmtr
tonypdmtr

Reputation: 3235

One more variation:

a=('A','1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1030')
b=('B', '-1.0', '1', '-2.443', '-1.456', '4.00', '3.00', '-2.343', '2.112', '3.00', '')

def average(values):
  sum = 0
  count = 0
  for num in values:
    try:
      sum += float(num)
      count += 1
    except: pass
  return sum / count

print('%7.2f'%(average(a)))
print('%7.2f'%(average(b)))

Upvotes: 0

Anuj
Anuj

Reputation: 1014

The TypeError results when you are doing operations on unsupported datatypes.

Python Docs link for TypeError

In this particular scenario, the TypeError is raised cause you are trying to execute addition on two different datatype String and Int/Float without explicitly converting them to proper datatype.

def average(values):
     total = 0
     for element in values:
         if not element.isalpha() and element:
             total+=float(element)

     return total/len(values)

Result

a=('A','1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1030')
b=('B', '-1.0', '1', '-2.443', '-1.456', '4.00', '3.00', '-2.343', '2.112', '3.00',)

average(a)
933.0909090909091

average(b)
0.5336363636363637

If we are to only average over the numbers and not the strings, the function changes to

def average(values):
     total = 0
     total_values = 0
     for element in values:
         if not element.isalpha() and element:
             total_values+=1
             total+=float(element)

     return total/total_values

Result

average(a)
1026.4

average(b)
0.6522222222222223

Upvotes: 0

rahul mehra
rahul mehra

Reputation: 438

Error you got here because you are trying to add int and string type element here:

sum is int type element of value 0

and element in values are string type.

just replace:

sum+=i

with:

sum+=float(i)

Upvotes: 0

Rakesh
Rakesh

Reputation: 82785

a=('A','1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1030')
b=('B', '-1.0', '1', '-2.443', '-1.456', '4.00', '3.00', '-2.343', '2.112', '3.00', '')

print sum(map(float, filter(None, a[1:])))/(len(a)-1)
print sum(map(float, filter(None, b[1:])))/(len(b)-1)

Result:

1026.4
0.587

Upvotes: 1

Jonathan De Wet
Jonathan De Wet

Reputation: 346

a=('A','1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029', '1030', '1030')
b=('B', '-1.0', '1', '-2.443', '-1.456', '4.00', '3.00', '-2.343', '2.112', '3.00',)

def avarage(value):
    sum=0
    value = list(value)
    value.pop(0)
    for i in value:
        sum+=float(i)
    avrg = sum / (len(value))
    return avrg

print(avarage(a))
print(avarage(b))

The problem was that the values in the list were still strings. To convert from a string to a number you need to use float()

Upvotes: 0

Related Questions