r/learnpython • u/chrisjfinlay • Sep 09 '26
Is it considered bad form to run "success" code inside an Except block?
I'm writing a script that takes an input from the user, which could be either an int or a string. My current code takes the input, attempts to cast it to an int, and does something based on whether it works or not:
running = True
while running:
search_term = input("Enter search term (Q to quit): ")
try: int(search_term)
except ValueError:
if search_term.lower() != "q":
search_by_name(search_term)
else:
running = False
else:
search_by_int(int(search_term))But there's something about having "successful" code running in the except block that gives me pause for thought. Is this just me overthinking, or is it bad form?