Q: I am getting Name Error in else statement So! I made a simple program that gets a file name as input and open it and read it.
and there is a special file name "mbox.txt", to open it user must insert the pin code, after that user can access the content of the file.
for this I made a if else condition, if fname=="mbox.txt":
insert pass
else:
open it normally,
and yes! I use try,except and finally, if the file does not find execute runs the except code otherwise  continue with finally.
when ever I insert unknown file name it runs the except code and gives the NameError.
fname=input("Enter the file name: ")
try:
   open_file=open(fname)
except:
 print("The File'",fname,"'Did not find Please Enter A Existing File")
finally:
   y=3
if fname=="mbox.txt": 
  while y>0:
    print("To Open This File You Need To Insert The Access Passward")
    line=int(input("Insert passward"))
    if line==8523:
       read= open_file.read()
       print(read)  
       break
    else:
        if y==3 :
            print("Access Denied!")           
            y=y-1
            continue
        elif y==2:
           print("Access Denied!")   
           y=y-1
           continue
        else:
           print("UNKNOWN USER DETECTED, THE PROGRAM TERMINATE IMMIDIATELY")   
           break
         
else:
 read= open_file.read()
 print(read)


error
Traceback (most recent call last):
  File "c:\Users\TRED WINGS\Desktop\py\tempCodeRunnerFile.py", line 30, in <module>
    read= open_file.read()
NameError: name 'open_file' is not defined


A: Here are some control flow suggestions. I am making some guesses:
#!/usr/bin/env python


MAX_ATTEMPTS = 3
attempts = 0

while True:
    fname = input("Enter the file name: ")
    try:
        open_file = open(fname)
        break
    except FileNotFoundError:
        print(f"{fname} could not be found, please enter a file that exists.")


if fname == "mbox.txt":
    print("To Open This File You Need To Insert The Access Password")

    while MAX_ATTEMPTS > 0:
        line = input("Insert password (numbers only): ")


        if not line.isdigit():
            print("Invalid password, must pass digits, e.g. 2388")

            # Uncomment next line to count invalid entries against attempts:
            # attempts += 1

            continue
        
        if int(line) == 8523:
            read = open_file.read()
            print(read)

            break
        if attempts >= MAX_ATTEMPTS:
            print("UNKNOWN USER DETECTED, THE PROGRAM TERMINATE IMMIDIATELY")

            break
        else:
            attempts += 1

            print("Access Denied!")
else:
    read = open_file.read()
    print(read)

File entry:

*

*Wrap the file entry in a while True loop to continue until it finds an existing file.

*Use try / catch pattern wrapping open(file, mode='r') and catching FileNotFoundError
Attempts:

*

*Use constant (all upper case) for max attempts


*Set attempts to 0


*Optionally added a type check to make sure numbers/digits are entered
Uncomment attempts += 1 if you want invalid entries to count toward retries


*In the if condition, check for exhausting the max number of retries first


*Remove continue unless its needed to skip the rest of the loops (if it's the last line in the loop's block, it a noop)
