+ 7

Error in python

Can someone help me with my bug in my code? This is the part: #exceptions something = input() # take an input try: # do something with the input something = (int(something)) # if it's not a number it's a ValueError print(something) print(someting) # make a mistake print(movies[something]) except ValueError: # if input isn't a number print("type in a number") except IndexError: # if number is too high print("number must be under 6") except NameError: # if I used a wrong variable print("something is wrong with something ;)") try: # do something with the input print(something + 1) # if it's not a number it's a TypeError print(something # another mistake except TypeError: # if input isn't a number print("type in a number") except SyntaxError: print("There's a bug in the code") https://sololearn.com/compiler-playground/chE9gTsNZeHl/?ref=app

12th Aug 2025, 3:32 PM
Jakob
Jakob - avatar
24 Antwoorden
+ 2
Jakob oh, I didn't realize it was intentional. The problem is that syntax errors are caught when the code is being parsed, so the rest of the code do not get executed if it's thrown. One way to get around this is to execute it separately. Here is a warranted use case for the eval or exec function. change line 128 to: exec("print(something # another mistake") or eval("print(something ") # another mistake
13th Aug 2025, 6:25 AM
Bob_Li
Bob_Li - avatar
+ 5
The first try-except block already raises an exception, and the code is no further interpreted. What behavior do you expect for your code?
12th Aug 2025, 3:47 PM
Lisa
Lisa - avatar
+ 4
As I understand it, it works like this: The SyntaxError is raised before the code is executed (when the code is parsed, that is: when it is checked if it is a valid code). The NameError is only checked at runtime, when the code is executed. So, the SyntaxError has a special role compared to the other Errors. Whenever a SyntaxError is encomluntered, the code will not be executed at all, and hence, other non-syntactical errors won't be raised.
12th Aug 2025, 4:16 PM
Lisa
Lisa - avatar
+ 4
Don't delete it :) But I'm afraid you can show the SyntaxError in the same demo as the other errors. Perhaps you could make an extra script to explain how SyntaxErrors are handled?
12th Aug 2025, 5:13 PM
Lisa
Lisa - avatar
+ 3
I assume that the syntax error has higher precedence than the name error and that's why we're sent to line 129 straight away. I will need to have a closer look when I am free!
12th Aug 2025, 3:56 PM
Lisa
Lisa - avatar
+ 3
Thanks! Is there another way to do what I want or should I just delete it?
12th Aug 2025, 4:19 PM
Jakob
Jakob - avatar
+ 3
Bob_Li I was rather referring to the demo itself: If the SyntaxError doesn't allow executing the script at all, a demo on errors and exceptions should veridically show this instead of raising exceptions just for the sake of raising exceptions because then it doesn't represent the intended use-case. (Just my thoughts; I respect your approach to it.)
13th Aug 2025, 4:57 PM
Lisa
Lisa - avatar
+ 2
I would like to trigger all except blocks for the demo.
12th Aug 2025, 3:49 PM
Jakob
Jakob - avatar
+ 2
Thank you very much! I can wait 😀
12th Aug 2025, 3:59 PM
Jakob
Jakob - avatar
+ 2
Jakob you forgot to close the parenthesis for print on line 128 128 print(something # another mistake ^ mistake here should be: 128 print(something) # another mistake it is a syntax error, but the error mesage pointed to the wrong part ....
12th Aug 2025, 10:24 PM
Bob_Li
Bob_Li - avatar
+ 2
I know, I wanted to trigger a Syntax Error. But I made it in a try except block
13th Aug 2025, 6:02 AM
Jakob
Jakob - avatar
+ 2
Jakob Here's how I would write the exceptions section so everything is displayed,. Use separate try-except blocks with different values for something.: #exceptions something = 'x' try: # convert to int print(int(something)) except ValueError as e: # if something isn't a number print("type in a number. ", e) try: # add 1 to something print(something + 1) # if it's not a number it's a TypeError except TypeError as e: # if input isn't a number print("type in a number. ", e) try: print(someting) # make a spelling mistake except NameError as e: # if I used a wrong variable name print("something is wrong with someting ;) ", e) something=1000 try: print(movies[something]) except IndexError as e: # if number is too high print("number must be under 6 ", e) try: #wrap print in exec or eval so it's pre-parsed. eval("print(something ") # missing closing parenthesis in print except SyntaxError as e: print("There's a bug in the code: ", e)
13th Aug 2025, 6:45 AM
Bob_Li
Bob_Li - avatar
+ 2
Thank you! This works. Can you explain it to a beginner?
13th Aug 2025, 7:21 AM
Jakob
Jakob - avatar
+ 2
Note that "eval()" and "exec()" are only for the purpose of making this demonstration work. In any other context, avoid using these functions. Doesn't "hacking" the demo kind of defy the purpose of showing how exceptions are raised by Python?
13th Aug 2025, 9:18 AM
Lisa
Lisa - avatar
+ 2
Lisa yes, it's a cheaty workaround. but I'm out if ideas on how handle SyntaxError without crashing main...😅 at least there's no user input access to eval in the code. and the way it's written, it's guaranteed to fail, so that would somewhat minimize the security risk.
13th Aug 2025, 9:39 AM
Bob_Li
Bob_Li - avatar
+ 2
Thank you very much for your help. I think I'll just take it out.
13th Aug 2025, 5:30 PM
Jakob
Jakob - avatar
+ 1
Sorry but for what is your code Rocky Mondal?
13th Aug 2025, 5:36 AM
Jakob
Jakob - avatar
+ 1
Sorry to ask again. Why exactly does it not work in my script, but would work in another? Is there perhaps a better way for a syntax error that doesn't stop the whole thing immediately?
13th Aug 2025, 5:37 AM
Jakob
Jakob - avatar
+ 1
Jakob you forgot the ')' in print() line 128 should be: ⬇️ this 128 print(something) # another mistake
13th Aug 2025, 5:58 AM
Bob_Li
Bob_Li - avatar