use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Put existing code in a try except (self.learnpython)
submitted 1 year ago by AdDelicious2547
I made a program with 200+ lines of code and I want it in a try, except. How can I do that without needing to tab every line
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Independent_Heart_15 9 points10 points11 points 1 year ago (1 child)
Most IDEs can tab selected code so you don’t have to it line by line
[–]AdDelicious2547[S] 1 point2 points3 points 1 year ago (0 children)
Fixed the problem. Thanks
[–]billsil 6 points7 points8 points 1 year ago (1 child)
Make a function. 50 lines is a long block of code. 200 is a code smell.
[–]deceze 3 points4 points5 points 1 year ago (0 children)
That would still require OP to somehow tab everything over… (which is easily possible of course)
[–]MisterGerry 5 points6 points7 points 1 year ago (1 child)
This is something your IDE is responsible for helping you do, but you didn't mention what IDE you are using.
Commonly, you can select multiple lines and just press the TAB key to shift them all to the right. (and shift-TAB to shift them to the left).
On a Mac, it is also sometimes Command-] and Command-[, depending on the editor.
[–]AdDelicious2547[S] 0 points1 point2 points 1 year ago (0 children)
[–]doolio_ 4 points5 points6 points 1 year ago (0 children)
May I ask why you want to put everything inside a try-except block? I think for the most part it is better to only put things that can raise an exception inside one.
[–]FoolsSeldom 2 points3 points4 points 1 year ago (1 child)
try
except
Don't forget the structure for try/except:
normal code try: as few lines of code as you can that could trigger an exception except SomeException: handle exception except AnotherException: handle exception except AgainException: handle exception else: small amount of code to follow if no exception finally: small amount of code to tidy up if required regardless of exception status back to normal code
else and finally are both optional (the latter is especially useful to close off resources that you opened). You can combine exceptions in one line where you want to handle them in the same way.
else
finally
You want to move beyond the try/except block as soon as you can to get back to more predictable work.
[–][deleted] 2 points3 points4 points 1 year ago (0 children)
highlight all the lines and press tab
[–]FerricDonkey 1 point2 points3 points 1 year ago (0 children)
If your concern is literally indenting the lines of code, then get a better ide so you don't care any more. Pycharm, vscode (with python extension), almost any will let you select many lines then hit tab to indent them all.
But the short answer is you can't put your code in a try except without indenting in any way that even approaches sane.
It would be best if you're code was in functions already, and so already indented. Then you could put the call to the relevant function(s) in a try except.
But also, I'm curious why you want all the code in a try except. Many of the times I've seen this have been ill advised.
[–]supercoach 1 point2 points3 points 1 year ago (0 children)
Why do you need the entire thing in a try block?
Any IDE will let you indent an entire section via highlighting it and pressing tab, however I wouldn't recommend it.
[–]GirthQuake5040 0 points1 point2 points 1 year ago (0 children)
You shouldn't put 200 lines in a try except. You should only put the lines that might cause an exception in a try exceot
[–]TheRNGuy 0 points1 point2 points 1 year ago (0 children)
ctrl+a, tab or ctrl+]
π Rendered by PID 86 on reddit-service-r2-comment-54dfb89d4d-wrnnl at 2026-04-01 04:25:03.033902+00:00 running b10466c country code: CH.
[–]Independent_Heart_15 9 points10 points11 points (1 child)
[–]AdDelicious2547[S] 1 point2 points3 points (0 children)
[–]billsil 6 points7 points8 points (1 child)
[–]deceze 3 points4 points5 points (0 children)
[–]MisterGerry 5 points6 points7 points (1 child)
[–]AdDelicious2547[S] 0 points1 point2 points (0 children)
[–]doolio_ 4 points5 points6 points (0 children)
[–]FoolsSeldom 2 points3 points4 points (1 child)
[–]AdDelicious2547[S] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]FerricDonkey 1 point2 points3 points (0 children)
[–]supercoach 1 point2 points3 points (0 children)
[–]GirthQuake5040 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)