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']}
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']}
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']}
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']}
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']}
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']}