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

Arbaaz Ali
  • 119
  • 2
  • 8
  • Yes, that's the right intuition. This might a duplicate this question: https://stackoverflow.com/q/49262379/245915 – nofinator Sep 27 '22 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 – juanpa.arrivillaga Sep 27 '22 at 17:41
  • What do you want your code to do for an input of `foo`? – Sören Sep 27 '22 at 18:27

2 Answers2

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")
  • You can find a discussion on whether or not it is good style to use try except clauses for flow control: https://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python – Ronald van Elburg Sep 27 '22 at 18:53
-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}")
  • 1
    That's not a trick, that's unreadable. Why didn't you move the last print into the else block? – Sören Sep 27 '22 at 18:27
  • 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` – Bhargav - Retarded Skills Sep 27 '22 at 18:30
  • 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? – Sören Sep 27 '22 at 19:09
  • 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. – Bhargav - Retarded Skills Sep 27 '22 at 21:09