Python Forum

Full Version: Tuple out of range
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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?
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*