0

I am currently doing my NEA and I am having problems with the login stage. This is my registration code:

def login_create():
        import pickle
        import os

        new_login_filename = 'new_login.dat'

        login_list = []

        if os.path.exists(new_login_filename):
           yeet = open(new_login_filename,'rb')
           pickle.load(yeet)
        new_login = username, password
        login_list.append(new_login)


        with open(new_login_filename,'ab') as wfp:
            pickle.dump(login_list, wfp)


        with open(new_login_filename,'rb') as rfp:
            login_list = pickle.load(rfp)

It saves the registered username and password into a .dat file called new_login. I can't seem to work out how to get the login to work though. This is what I currently have:

def login():
        import pickle
        usernameV = False
        passwordV = False
        username_login = input("Enter your Username")
        password_login = input("Enter your Password")
        f = open("new_login.dat","rb")
        for i in range(len(new_login)):
            if username_login in new_login[i][0]:
                        print("Valid Username")
                        usernameV == True

            elif password_login in new_login[0][i]:
                        print("Valid Password")
                        passwordV == True
            elif usernameV == True and passwordV == True:
                        valid = True
                        return valid
        f.close()

I get errors with new_login not being defined and then it just doesn't work at all. Any help will be greatly appreciated as I have been trying to work this out and I have been looking everywhere and nothing I do seems to work.

Tabitha
  • 13
  • 4
  • When asking about code that produces an Exception you should always include the complete Traceback in the question. Copy the Traceback and paste it in the question, then format it as code (select it and type ctrl-k). – wwii Jan 13 '19 at 06:06

1 Answers1

0
f = open("new_login.dat","rb")`
for i in range(len(new_login)):
    ...

You assigned the file object to the name f then in the next line you use the name new_login - did you intend to assign it to new_login? The canonical way to iterate over a file is

with open("new_login.dat","rb") as new_login:
    for line in new_login:    #you don't have to use "line" you can use whatever name you want
        if username_login in line[0]:
            ....
wwii
  • 19,802
  • 6
  • 32
  • 69
  • Oh yes, I did mean to I forgot! Thanks, the issues with it being defined are now sorted. The problem now is that it will not stop you if the username and password don't match, or in fact are even in the file. I don't think it checks it properly maybe I have accidentally skipped something. I will check through again and see if I can work it out :) – Tabitha Jan 13 '19 at 06:35
  • You should ask a separate question. You should include enough information in that question for an answer-er to test your code and easily test a solution - always include an example of the input, a few lines of the `.dat` file, formatted as code. ... [mcve] – wwii Jan 13 '19 at 15:58