How To Keep Error Handling Code Focused // Python Tips

preview_player
Показать описание


🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

Even though I always appreciate the tips here, in this case I think a separate function for reading in the integer is over engineered. The try/except block has an else branch for just such cases to bring the exception close to the trigger and still generate readable code, IMHO the better code in this case looks like this.


try:
guess = int(input("Guess a number: " )
except ValueError:
print("Thats not a number")
else:
if guess == answer:
print("You win")
elif guess < answer:
print("Too low!")
else:
print("Too big!")

oliverschneidewind
Автор

I'm really enjoying these short coding videos

cipher
Автор

Do you plan to continue the code roast refactoring videos? I really miss that series

mateusb
Автор

If the user enters an alphabet, code will just print that’s not a number in a loop. while loop of read_int doesn’t have an exit condition for exception

RGauthamRam
Автор

Hi Arjan, Could you make a video on designing a "Parking Lot" or "Design Elevator". It'll be terrific to see your version of the design for the standard design question

rakesha
Автор

Great tip! 😄 But to make it real clean, you'd need to inject a parser error handling function into the parser function and call it on error instead of rethrowing the exception. That way you can decouple the parser logic from the error handling behavior 😉 (But I know it's a bit too much info for a short 😂)

marcotroster
Автор

The read_int fnctuion is suppossed ro return int. What does it return in case of the exception? Would not the place_a_guess function say "Too high!"?

bruxy
Автор

I absolutely love these short tips videos! But in this case I feel like putting one line of code in its own separate function just to handle exceptions seems a bit overkill and added complexity for the code. But I'm by no means an experienced programmer, so anyways hehe

laml
Автор

3..14 is a number between 1 and 10 yet the exception will say it is not a number, rather than the orignal Python exception that was more specific.

dmail
Автор

Do you put the code for these shorts on git-hub? I try to make a short write up for each video you make, so, lazily, I'd like to copy your before and after code examples.

hughe
Автор

the while loop in the read int function 😹

jvyt