Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple out of range
#1
Hey, so I've looked around to try and identify why this apparent return value is causing a tuple out of range error, I've yet to come up with a solution. Here's my code:
embed.set_thumbnail(url = "https://img.youtube.com/vi/{0}/default.jpg".format(id = await youtubeurlsnipper(vplayer.url)))


async def youtubeurlsnipper(x):
    url = x
    if len(url) > 43:
        url = url[32:43]
    else:
        url = url[32:]
    return url

Never mind, just figured it out by myself. I think what was happening was it was trying to have 2 index values be used in the format when there was actually only one. I removed id = and it works fine now.
Reply
#2
just a side note - you don't need the if block in the function. It will not rise error even if len is less than second index
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> mystring='abc'
>>> mystring[1:5]
'bc'
i.e. you can just use url[32:43] whatever the len of url is.
Note that your function will not return consistent result - if len(url)>43 it will return chars 32-42 (i.e. 43 is not included), i.e. string of len 11.
If len(url)=43 it will return string of len 12 (32-43, inclusive)
and finally comes the question why using async?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It's async because it's a bot that has other functions running at the same time. And honestly it's easier to have everything run async where it can rather than have everything running in a linear fashion. More work that I want to put into it. And thanks for the info on slicing :P.

More work than I want to put into it*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,308 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  matplotlib x axis range goes over the set range Pedroski55 5 3,175 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,802 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Get the codemap combinations but got tuple index out of range haganmao 1 1,970 Feb-21-2020, 02:51 AM
Last Post: michael1789
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,017 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,187 May-21-2019, 11:39 AM
Last Post: avorane
  tuple index out of range delta1071 3 6,759 Apr-11-2019, 07:15 AM
Last Post: snippsat
  IndexError: tuple index out of range Truman 5 14,732 Mar-27-2018, 10:38 PM
Last Post: Truman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020