[Tutor] Program help

Alan Gauld alan.gauld at yahoo.co.uk
Fri Oct 23 15:07:14 EDT 2020


On 23/10/2020 19:25, alexkleider via Tutor wrote:

>>     Convert it to numbers:
>>
>>     for x in range(data):
>>      data[x]=int(data[x])

> The for loop didn't seem quite right to me since data appears to be a list.

>>>> d = ['1', '3', '4']
> 
>>>> for x in range(d):
> ...   data[x] = int(d[x])
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'list' object cannot be interpreted as an integer
>>>>
> 
> ... or am I missing something?
Its missing a len()

for x in range(len(data)):
    data[x] = int(data[x])

But better still would be a list comprehension

data = [int(n) for n in data]


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list