[Tutor] lists

Michael Haft m.haft at abdn.ac.uk
Tue Feb 14 16:15:54 CET 2006




Hello,
        I have a list of 15 str values called p, when I try the following code:

for x in p:
     p[x] = float(p[x])/10
print p

I get:

Traceback (most recent call last):
   File "C:/Python24/CRU_2_Century code/CRU_2_DNDC.py", line 62, in -toplevel-
     p[x] = float(p[x])/10
TypeError: list indices must be integers

If it helps p=['1900', '51.50', '11.50', '15.30', '11.80', '5.00', '17.40', 
'25.30', '7.90', '1.00', '2.20', '12.10', '26.00', '10.00', '8.80']

I'm confused because if I try it without the for loop, i.e.:

x = 3
p[x] = float(p[x])/10
print p

It works just fine, I assume it doesn't like me floating the value but why 
does it work out of the loop but not in the loop?

Cheers

Mike

------------
Michael Haft
School of Biological Sciences
Plant & Soil Science
Cruickshank Building
St.Machar Drive
Aberdeen AB24 3UU
E-mail: m.haft at abdn.ac.uk



More information about the Tutor mailing list