View Full Version : there a command to sum up values from a [list]?


naf4ever
Mar 14, 2006, 04:40 PM
First off thanks to everyone who's been helping me so far. This is what my code looks like at the moment:

sSci = []
player = gc.getActivePlayer()
for i in range(player.getNumCities()):
sSci.append(i)
sPoints = (player.getCity(sSci[i]).getSpecialistCommerce(1))

The code works without errors, but doesnt do what i want it to. Basically im trying to get the total science output from specialists from all my cities and then add them up to see their total. I thought sPoints would do this and return back the summed up values of the specialist science output from the cities in list sSci = [] . But it only returns back the output from the last city I built or acquired.

Anyone know how i can sum these values instead?

talchas
Mar 14, 2006, 04:51 PM
Ok, first of all, you have no need for sSci - you are just slowly duplicating range(player.getNumCities). Second, if you make that sPoints = sPoints + ... or sPoint += ... then it will work.

naf4ever
Mar 14, 2006, 05:45 PM
Sorry Im a newb at this. I think I get what your saying but im not sure how to implement it. I got rid of the list then added this line:
sPoints = (sPoints + (player.getCity(i).getSpecialistCommerce(1)))

I dont think this is what you meant though because this line simply doubles the value if i have only one city. And once i get a second city it suffers the same problem of not returning any value because the (i) gets focused on the city that was just built and ignores all my other cities.

12monkeys
Mar 14, 2006, 06:06 PM
What talchas meant is :


iPoints = 0
player = gc.getActivePlayer()
for i in range(player.getNumCities()):
iPoints += player.getCity(i).getSpecialistCommerce(1)


The result in iPoints should be the sum of all specialitst commerce (1) (whatever the 1 means here).

naf4ever
Mar 14, 2006, 06:13 PM
That did it! Thanks again 12Monkeys and Talchas....