#๐Ÿ”’ "IndexError: list index out of range"

9 messages ยท Page 1 of 1 (latest)

willow trout
#

I'm a little bit puzzled on solving this error because on first look it doesn't appear anything should be wrong.

Code snippet:

            query = "SELECT unix, credits, s_company, member, r_company, s_user, r_user FROM transfers WHERE s_company = %s OR r_company = %s"
            values = (name, name)
            cursor.execute(query, values)
            transfers = cursor.fetchall()

            if transfers:
                print(transfers)
                transaction_logs = []
                for transfer in transfers:
                    unix = transfers[0]
                    credits = transfers[1]
                    s_company = transfers[2]
                    member = transfers[3]
                    r_company = transfers[4]
                    s_user = transfers[5]
                    r_user = transfers[6]

                    unix = f"**<t:{unix}:d>**"
                    
                    if s_company == name:
                        if r_company != 'None':
                            transaction_logs.append(f"{unix} <@{member}> sent {credits:,} to {r_company}")
                        else:
                            transaction_logs.append(f"{unix} <@{member}> sent {credits:,} to {r_user}")
                    else:
                        if s_company != 'None':
                            transaction_logs.append(f"{unix} {s_company} sent {credits:,}")

                        else:
                            transaction_logs.append(f"{unix} {s_user} sent {credits:,}")

Error:
[Bot] [(1719570961, 1, 'None', 0, 'CredCorp', 606134062125678595, 0), (1719571435, 1, 'None', 0, 'CredCorp', 606134062125678595, 0)]
(error starts here)
[Bot] File "/bot.py", line 1059, in company_balance
[Bot] s_company = transfers[2]
[Bot] IndexError: list index out of range
[Bot] The above exception was the direct cause of the following exception:
[Bot] nextcord.errors.ApplicationInvokeError: Command raised an exception: IndexError: list index out of range

pallid quailBOT
#

@willow trout

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

steel whale
willow trout
radiant fjord
steel whale
#

Lists and tuples are indexed the same way.

pallid quailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.