problem adding list values

D

David M. Synck

Hi all,

I am fairly new to Python and trying to figure out a syntax error
concerning lists and iteration through the same. What I am trying to do is
sum a list of float values and store the sum in a variable for use later.

The relevant code looks like this -

def getCredits():

""" This function asks the user to input any credits not shown on their bank statement

It returns the sum(converted to float) of the entered credits """

global credits
credlist = []
credits = 0.0
temp = 0.0
print "Now you need to enter any credits not shown on your bank statement \n"
print "Please enter a zero (0) once all credits have been entered \n"
raw_input("Hit 'Enter' to continue \n")
temp = float(raw_input("Please enter the first credit \n"))

while temp != 0:
credlist.append(temp)
temp = float(raw_input("Please enter the next credit \n"))

i = 0
for i in credlist:
credits += credlist
i = i + 1

return credits

And the syntax error I get is this -

Traceback (most recent call last):
File "./BankReconciler_Rev1.py", line 129, in ?
main()
File "./BankReconciler_Rev1.py", line 116, in main
getCredits()
File "./BankReconciler_Rev1.py", line 60, in getCredits
credits += credlist
TypeError: list indices must be integers


If anyone can point me in the right direction, I would greatly appreciate
it.

Thanks in advance
 
G

Gerard Flanagan

David said:
Hi all,

I am fairly new to Python and trying to figure out a syntax error
concerning lists and iteration through the same. What I am trying to do is
sum a list of float values and store the sum in a variable for use later.

The relevant code looks like this -

def getCredits():

""" This function asks the user to input any credits not shown on their bank statement

It returns the sum(converted to float) of the entered credits """

global credits
credlist = []
credits = 0.0
temp = 0.0
print "Now you need to enter any credits not shown on your bank statement \n"
print "Please enter a zero (0) once all credits have been entered \n"
raw_input("Hit 'Enter' to continue \n")
temp = float(raw_input("Please enter the first credit \n"))

while temp != 0:
credlist.append(temp)
temp = float(raw_input("Please enter the next credit \n"))

i = 0
for i in credlist:
credits += credlist
i = i + 1

return credits



David

replace these lines:

i = 0
for i in credlist:
credits += credlist
i = i + 1

with these:

for i in credlist:
credits += i


the 'for' loop handles the indexing for you. maybe read 'for' as
'foreach' until it becomes more familiar.

Hope that helps.

Gerard
 
D

Dave Hansen

On Thu, 22 Dec 2005 19:43:15 GMT in comp.lang.python, "David M. Synck"

[...]
temp = float(raw_input("Please enter the first credit \n"))

while temp != 0:
credlist.append(temp)
temp = float(raw_input("Please enter the next credit \n"))

Here you're building credlist as a list of floats.

This is wasted effort
for i in credlist:

You've asked to loop through credlist, so each value of i is going to
be float. Maybe you should rename i to something that looks less like
an index and more like a credit.
credits += credlist


Here, you're trying to indexa list with a float. No can do...
[...]
TypeError: list indices must be integers

....ss it's telling you here
If anyone can point me in the right direction, I would greatly appreciate
it.

I think what you want is

for cr in credlist:
credits += cr

Which could also be implemented as

credits = reduce(lambda x,y: x+y, credlist)

HTH,
-=Dave
 
A

andy

David said:
Hi all,

I am fairly new to Python and trying to figure out a syntax error
concerning lists and iteration through the same. What I am trying to do is
sum a list of float values and store the sum in a variable for use later.

The relevant code looks like this -

def getCredits():

""" This function asks the user to input any credits not shown on their bank statement

It returns the sum(converted to float) of the entered credits """

global credits
credlist = []
credits = 0.0
temp = 0.0
print "Now you need to enter any credits not shown on your bank statement \n"
print "Please enter a zero (0) once all credits have been entered \n"
raw_input("Hit 'Enter' to continue \n")
temp = float(raw_input("Please enter the first credit \n"))

while temp != 0:
credlist.append(temp)
temp = float(raw_input("Please enter the next credit \n"))

i = 0
for i in credlist:
credits += credlist
i = i + 1

return credits

And the syntax error I get is this -

Traceback (most recent call last):
File "./BankReconciler_Rev1.py", line 129, in ?
main()
File "./BankReconciler_Rev1.py", line 116, in main
getCredits()
File "./BankReconciler_Rev1.py", line 60, in getCredits
credits += credlist
TypeError: list indices must be integers


If anyone can point me in the right direction, I would greatly appreciate
it.

Thanks in advance

Your problem is here:

i = 0
for i in credlist:
credits += credlist
i = i + 1

In the for loop, i is successively bound to each element in credlist
(which are floats) and you then try to index credlist with each element
in turn: you can't index lists with floats, so you get an error.

Try inserting a print command just before the credits expression, and
you'll see what I mean:

i = 0
for i in credlist:
print i
credits += credlist
i = i + 1


What you probably mean is:

credits = 0.0
for i in credlist:
credits += i

However, you should really use:

credits = sum(credlist)

It's far faster and more "pythonic".

NOTE: Although it may be easy to think of Python lists as arrays,
they're more than that. The Python for loop moves through a list,
binding (think assigning) the loop variable (in this case "i") to each
*element* of the list at a time on successive iterations, viz:
for i in ["apples","oranges","grapes","pears","tomatoes"]:
.... print i
apples
oranges
grapes
pears
tomatoes
Hope that helps ;-)

-andyj
 
M

mrmakent

Glad to help. Your relevant code:
i = 0
for i in credlist:
credits += credlist
i = i + 1


credlist is your list of credits, which are floating point numbers.
You use a for-loop to iterate thru this list of floating point numbers,
so each time thru the loop, the variable 'i' is set to the next
floating point number from the list. 'i' is NOT being set to a
sequence of integers starting with '0'. You then try to index into
credlist using the floating-point number set in 'i' as the index, which
you can't do. I presume you are used to the for-loop behavior from
some other language.

To rewrite this code to correctly use a for-loop, try something like:

totalCredits = 0.0

for credit in credlist:
totalCredits += credit

Alternatively, you can use the build-in function 'sum' to sum a
sequence of numbers:

totalCredits = sum(credlist)

Python isn't a hard language to pick up, but there are some significant
differences from other languages like C or Basic. You're off to a good
start.
 
T

Tomasz Lisowski

Dave said:
I think what you want is

for cr in credlist:
credits += cr

Which could also be implemented as

credits = reduce(lambda x,y: x+y, credlist)

or even:

credits = sum(credlist)

Tomasz Lisowski
 
G

gene tani

David said:
""" This function asks the user to input any credits not shown on their bank statement

(OT I know, but just so you know, you *may* get away with using floats
for financial calculations if you're handling small numbers of floats
of roughly same order of magnitude, but if your calculations have to
be exact to the penny, and you have lots of numbers not of the same
magnitudes, you probably don't want to do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top