Can You Use If Statement within Try block | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can You Use If Statement within Try block

An example to illustrate the title of this post: def input_number(): while True: try: user_inp = int(input("Enter a number between 1 to 5: ")) if user_inp > 5: print("Only between 1 to 5!") except ValueError: print("Do not leave field empty!") else: return user_inp Is it...Pythonic to do it this way? Because I tried to resolved it using only if statement to account for empty string but it didn't work..hence, the exception handling

14th Aug 2021, 7:22 AM
Alex
Alex - avatar
6 Answers
+ 2
Well, in my opinion, efficiency isn't very important in Python since It is already not efficient compare to other languages. If I want efficiency, I won't code in Python. Python is slow. In exchange, Python improves readability and flexibility a lot so it's easier to maintain and read codes. I'm not a Pythonist myself, but I suggest you can raise an exception in the if statement with the error message. It can seperate the logic of your function and error handling, so whenever you want to update the error handling, just go to the except block.
14th Aug 2021, 8:57 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Your code works.
14th Aug 2021, 7:26 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Thank you for replying and yes, it worked, but I'm asking in terms of efficiency in the code. I'm sure there is a much better alternative than this because I can't seem to find answers regarding using "Try...If...Except...Else".
14th Aug 2021, 7:36 AM
Alex
Alex - avatar
+ 1
I don't remember if try statements can end with "else", but seeing the program you're trying to make, you'd probably want to put the else block right after the if block However, if try statements can end with "else", then yes, the code would be valid. In terms of efficiency, the example given also would work, as in this example code, entering a number above 5 isn't something that would bring an error, but you need a number from 1-5 in the code. An if statement would likely be the best to use in this example.
14th Aug 2021, 11:59 AM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 1
The "except" block is already catching those errors. For example, a message will be printed out to the user if they did not input any value onto the prompt. And about the "Try...Except...Else" statements, "else" and "finally" are optional, according to Python's doc on Errors and Exceptions, it stated, "The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement." Hence, the "else" statement. Anyways, I found a few workarounds by including the code in other functions, worked the same. Thank you guys for the help and responses :D
14th Aug 2021, 2:15 PM
Alex
Alex - avatar
0
You're welcome!
14th Aug 2021, 2:22 PM
PresidentOfYes12
PresidentOfYes12 - avatar