Using variable 'file' before assignment

try:
    file = open("app.py")
    age = int(input("Age: "))
    xfactor = 10 / age
except (ValueError, ZeroDivisionError):
    print("you didn't enter a valid age.")
else:
    print("No exceprions were thrown.")
finally:
    file.close()

PyLint highlights with red “file.close()” in finally block and issues a warning “Using variable ‘file’ before assignmentPylint(E0601:used-before-assignment)”. I need help to understand what’s wrong) I tried to execute this code a few times, and everything worked great.

To address this issue, you can declare the file variable
Example at the beginning:

#Initialize the variable with a default value
file = None or File = “”

Info
E0601 used before assignment:
Description: Emitted when a local variable is accessed before its assignment took place. Assignments in try blocks are assumed not to have occurred when evaluating associated except/finally blocks.

Details:
https://pylint.readthedocs.io/en/latest/user_guide/messages/error/used-before-assignment.html

Seems like the the warning you’re getting, Using variable ‘file’ before assignment (E0601:used-before-assignment), you mistakenly edited your code as it worked great. You may be using the wrong variable. Don’t worry, if you are still not getting it. You can prefer CIPD level 3 assignment help UK to write your assignment for you.