0

I am a bit new in python, and have just learned about the try except else and finally statement.

try: x=int(input("enter a number:") except: print("you entered a wrong type of input, make sure to enter an integer value") else: print(f"running in else : {x}") finally: print(f"finally : {x+2}") 

This will cause another exception in finally block NameError name 'x' is not defined if I enter anything other than an integer value in my input statement

Does it mean we have to put all that is related to x in the else block and all that have nothing to do with x, in the finally block?

My actual script is very long but I'm trying to understand this concept from a smaller example

Is this a right intuition?

Please suggest me if otherwise

3
  • Yes, that's the right intuition. This might a duplicate this question: stackoverflow.com/q/49262379/245915 Commented Sep 27, 2022 at 17:31
  • " and all that have nothing to do with x, in the finally block?" No, you just have to make sure that x will actually be defined if you use it in the finally block Commented Sep 27, 2022 at 17:41
  • What do you want your code to do for an input of foo? Commented Sep 27, 2022 at 18:27

2 Answers 2

0

I am inclined to think that you would want to give the user a second chance, and a third, and ...

So why not wrap it in a while-loop to enforce the presence of an x which is an integer at the end:

x=None while x is None: try: x=int(input("enter a number:")) except ValueError: print(f"You entered a wrong type of input, make sure to enter an integer value") 
Sign up to request clarification or add additional context in comments.

1 Comment

You can find a discussion on whether or not it is good style to use try except clauses for flow control: stackoverflow.com/questions/16138232/…
-1

Finally blocks executes No matter what (unless you destory the laptop/PC).

No matter what happened previously, the final-block is executed once the code block is complete and any raised exceptions handled. Even if there's an error in an exception handler or the else-block and a new exception is raised, the code in the final-block is still run.

But, In this case you can use flags inside the final statement.

final = True #Flag true try: x=int(input("enter a number:")) except: print("you entered a wrong type of input, make sure to enter an integer value") final = False #Flase if except else: print(f"running in else : {x}") finally: if final: print(f"finally : {x+2}") 

4 Comments

That's not a trick, that's unreadable. Why didn't you move the last print into the else block?
Yes, You can...I don't want to change users code...As user mentioned he has some complicated code & Trying to understanding with same similar exapmle that he has...So, Just recomended using flags
But you did change the code. You introduced a completely unnecessary flag. Why not use the opportunity to improve the code? You know, in case OP follows your advice?
I did add some logic to the code. But, "HAVE NOT CHANGED ANY STRUCTURE OF THE CODE". I never mentioned it was final or one and only option to solve the problem. I just don't WANT TO CHANGE CODE FORMAT OF THE OP. OP is saying he just given some similar format example of complex code he has. Who knows final block may contains other related part of other logic.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.