+ 1

How can I add exception in my code if user is not putting a number in input

https://sololearn.com/compiler-playground/c7kKZlI5bmvY/?ref=app

9th Apr 2025, 4:46 PM
Girish Sahu
Girish Sahu - avatar
2 Answers
+ 8
Girish Sahu , this is the basic structure for handling a single exception: ... try: # code that could raise an exception ... except: # message or code to handle the exception ... # continue code ... > so in your case, the input() function with the int() conversion should be in the try block. > the except block should give an error message. >> the exception handling could be improved for more advanced cases. also try a search in the code area for ``exception``. you may find some helpful code samples.
9th Apr 2025, 5:40 PM
Lothar
Lothar - avatar
+ 5
did you try "try-catch"? in python is: try-except note: also you have a finally keyword as well
9th Apr 2025, 4:50 PM
Mihaly Nyilas
Mihaly Nyilas - avatar