How do I fix this syntax error on my Try and Except

Question:

I am making a discord bot and I am adding a music feature but when I run it it gives me a syntax error on my exception and I couldn’t find a solution.

def search_yt(self,item):
  with YoutubeDL(self.YDL_OPTIONS) as ydl:
    try:
       info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'[0]
    except Exception:
          return False
  return {'source': info['formats'[0]['0']], 'title': info['title']}

enter image description here

Asked By: James

||

Answers:

You are using mixed indentation and there’s a missing ] on the fourth line.
Here’s a properly formatted code (though it has other issues, so will probably not work):

def search_yt(self,item):
    with YoutubeDL(self.YDL_OPTIONS) as ydl:
        try:
            info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'[0]]
        except Exception as e:
            return False
        return {'source': info['formats'[0]['0']], 'title': info['title']}
Answered By: Matas Minelga

First of all your code is not spaced well use TAB and not spaces(I would recement you to use the indent-rainbow module on visual studio.
Second you have forgotten an ] on line 4

def search_yt(self,item):
    with YoutubeDL(self.YDL_OPTIONS) as ydl:
        try:
            info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'[0]]
        except Exception:
            return False
    return {'source': info['formats'[0]['0']], 'title': info['title']}
Answered By: PanosoikoGr
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.