Python Tip #292 (of 365): Keep "try" blocks small with an "else" clause. try: co...
Trey Hunner ๐ @treyhunner@mastodon.social
Python Tip #292 (of 365):
Keep "try" blocks small with an "else" clause.
try:
count = int(text)
except ValueError:
print("That's not a number")
else:
print(f"Counting to {count}")
The "else" block runs only if the "try" block finished without raising an exception.
Why not put that print call at the bottom of the "try" block instead?
try:
count = int(text)
print(f"Counting to {count}")
except ValueError:
print("That's not a number")
๐งต (1/3)