problem adding list values
Dave Hansen
iddw at hotmail.com
Thu Dec 22 15:25:44 EST 2005
On Thu, 22 Dec 2005 19:43:15 GMT in comp.lang.python, "David M. Synck"
<dmsynck at verizon.net> wrote:
[...]
> 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.
>
> i = 0
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[i]
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
--
Change is inevitable, progress is not.
More information about the Python-list
mailing list