from math import *
import sqlite3
conn = sqlite3.connect('person.sqlite3')
def main():
agelist = conn.execute("SELECT age from person where age!='NA'")
ages = []
for row in agelist: ages += [row [0]]
sumthis = []
for row in agelist:
sumthis += [row[0**2]]
sqrted=sum(sumthis)
print(sqrted)
I am trying to square every row of data in agelist, and find the sum of all of those squared numbers. Right now this is giving me 0 as an answer. I want sum(age^2 for each age in ages list) How can I correct this?
I think, you should replace
sumthis += [row[0**2]]
With,
sumthis += [row[0]**2]
Or, more appropriately,
sumthis.append(row[0]**2)
That's because, forming a new list and add two list at every iteration isn't a good idea.
For the same reason, change
for row in agelist: ages += [row [0]]
To:
for row in agelist: ages.append(row [0])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.