Jessica Smith - 1 year ago 174

Python Question

`from math import *`

import sqlite3

ages=sqlite3.connect('person.sqlite3')

def main():

ageslist=ages.execute("SELECT age from person")

#average age

for row in ageslist:

row[0]

average = (sum(row[0]))/len(row[0])

#subtracts average x from x or opposite and square, depending on n

for n in range(len(ageslist) - 1):

if numbers[n] > average:

numbers.append((ageslist[n] - average)**2)

if numbers[n] < average:

numbers.append((average - ageslist[n])**2)

#takes square rt of the sum of all these numbers and divides by n-1

Stdv = math.sqrt(sum(ageslist))/(len(ageslist)-1)

end=time()

print(Stdv)

main()

I am trying to find the standard deviation of the ages from an SQLITE3 db. However, I am getting the current error:

average = (sum(row[0]))/len(row[0])

TypeError: 'int' object is not iterable

How can I correct this?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The query sent to the database connection returns an iterator. You can only pass over that iterator once before it is flushed from memory. Here is some correction to your code to do what you are asking.

```
conn = sqlite3.connect('person.sqlite3')
def main():
ages_iterator = conn.execute("SELECT age from person")
# this turns the iterator into an actual list, which you need for stdev
age_list = [a[0] for a in ages_iterator]
# average age
average = (sum(age_list))/len(age_list)
# subtracts average x from x square
# because you are squaring the difference, the it does not matter if it is
# greater or less than the average
numbers = [(age-average)**2 for age in age_list]
#takes square rt of the sum of all these numbers and divides by n-1
Stdv = math.sqrt(sum(numbers))/float(len(numbers)-1)
end=time()
print(Stdv)
main()
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**