Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

Python Discord Bot - IndexError: list index out of range

I'm new to python (and programming in general), and I'm making a discord bot that will help me with my school schedule. So I want to type like !schedule to get my schedule for the day. I have a link to a "API" that my friend made which is "https://apischoolsoft.herokuapp.com/v1/schedule/TE19?today=true".

Here's the code:

lessons = str('https://apischoolsoft.herokuapp.com/v1/schedule/TE19? 
today=true')
lessons = lessons.replace('och', '')
lessons = lessons.replace('}', '')
lessons = lessons.replace(']', '')
lessons = lessons.replace("'", '')
lessons = re.sub( '(?<!^)(?=[A-Z])', ' ', lessons )
lessons1 = lessons.split(",")

And so I was also given this code by someone else:

async def response2():
embed = discord.Embed(
title = 'Schema',
description = '',
colour = discord.Colour.blue()
)
embed.set_author(name='Schoolsoft')
embed.add_field(name='1', value=lessons1[1], inline=False)
embed.add_field(name='2', value=lessons1[2], inline=False)
embed.add_field(name='3', value=lessons1[3], inline=False)
embed.add_field(name='4', value=lessons1[4], inline=False)
embed.add_field(name='5', value=lessons1[5], inline=False)
return embed

But it says "Command raised an exception: IndexError: list index out of range", and I believe it's the value for lessons1 thats making it troublesome. I wrote the code below to call for this function:

@client.command(name='schema', help='This command returns schema')
async def schema(ctx):
# responses = schoolsoft_api.get_lessons(token, school, org_id)
#await ctx.send(r.text)
#embed.add_field(name="Field1", value="hi", inline=False)
embed = await response2()
await ctx.send(embed=embed)

I hope someone can help me with this since it would be really cool to get it work. What I basically want is that it will list everything from the link embedded instead of raw for a better look. Thanks in advance!

question from:https://stackoverflow.com/questions/66052026/python-discord-bot-indexerror-list-index-out-of-range

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error pretty much means that you're trying to access a value at some index in the list that doesn't exists, explanation

>>> some_random_list = [1, 2, 3]
>>> some_random_list[0] # List indexes start at 0, not 1
1
>>> some_random_list[3]
IndexError: list index out of range

I'm guessing you're getting the error when adding the fields to the embed in the response2 function, make sure you didn't mess up the indexes and the list is of the correct length, you can also use a try/except block


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...