TypeError: list indices must be integers

Mensanator mensanator at aol.com
Tue Dec 30 21:05:53 EST 2008


On Dec 30, 7:46�pm, dubux <wx1... at gmail.com> wrote:
> here is a function i wrote that doesnt work. i wrote to parse a "news"
> file that is going to work in conjunction with a website via mod_wsgi.
> my head hurts from looking at it so long. please help! i will further
> explain in the post.
>
> def news(x,y):
> � � � � # usage news(date, number)
> � � � � # x = date/news
> � � � � # y = number
> � � � � news_file = '/home/scam/Desktop/www/info/news'
> � � � � news = open(news_file, 'r')
> � � � � news_list = news.readlines()
> � � � � news.close()
> � � � � if x == 'date':
> � � � � � � � � mylist = map(lambda i: news_list[i], filter(lambda i: i%2 == 0, range
> (len(news_list))))
> � � � � � � � � date = mylist[y]
> � � � � � � � � return '<center>%s</center><br>' % (date)
> � � � � if x == 'news':
> � � � � � � � � mylist = map(lambda i: news_list[i], filter(lambda i: i%2 == 1, range
> (len(news_list))))
> � � � � � � � � news = mylist[y]
> � � � � � � � � return '%s<br>' % (news)
> � � � � else:
> � � � � � � � � return news_list
>
> and call it with the follow syntax: news('[date/news]', [any number])
>
> i keep getting "TypeError: list indices must be integers" on the
> following line "date = mylist[y]"
> can someone please explain this and give me the proper way to achieve
> what im trying to do?

The code you posted assumes y is an integer. The TypeError message
shows that assumption is false. Nothing in this code will help you
resolve the problem, it's in the code that calls this code.

Throw a print type(y),y into tha start of the definition.



More information about the Python-list mailing list