nitay prozanski
nitay prozanski

Reputation: 11

read file and write to file error in python

well I'm getting this error when I try to use login option "local variable 'register' referenced before assignment"

def logins():
    choice = int(input("1) Do you want to Login?: \n2) Do you want to Register?: \n"))
    
     if choice == int(2):
        info = input("choose UserName: \n")
        password = input("choose PassWord: \n")
        register = open("D:\login\Login.txt", "r+")
        register.write(info + "\n" + password)

    if choice == int(1):
        infos = input("choose UserName: \n")
        passwords = input("choose PassWord: \n")
        if register == str(infos):
            print("Welcom Back! ")
            #register.close()


logins()

Upvotes: 1

Views: 130

Answers (1)

Sharim09
Sharim09

Reputation: 6224

I think this code may help you.

As you use register variable in both case, then you can defined this variable outside the if statement.

def logins():
    choice = int(input("1) Do you want to Login?: \n2) Do you want to Register?: \n"))
    
    register = open("D:\login\Login.txt", "r+")
    if choice == 2: # 2 is already int no need to change it to int.

        info = input("choose UserName: \n")
        password = input("choose PassWord: \n")
        register.write(info + "\n" + password)



    if choice == 1: # 1 is already int no need to change it to int.
        infos = input("choose UserName: \n")
        passwords = input("choose PassWord: \n")
        if register == str(infos):
            print("Welcom Back! ")
    register.close()




logins()

Upvotes: 2

Related Questions