0

My program is a basic username password system I am working on. It allows you to store a username and password in a pickle file, and then log in with it. However, only the first "account" I make in the file works. I have looked at similar problems, but none of the solutions appear to work. Is there anything I am doing wrong, or need to add so all the accounts work? I have decoded the pickle file and all the accounts are there. I am a beginner programmer, so please give a simple explanation. Here is my code:

import pickle
import sys
l=0
answer="."
c=0
while c==0:
    if not answer=="y" or not answer=="yes" or not answer=="Yes" or not answer=="n" or not answer=="No" or not answer=="no":
        answer=input("have you already created an account?(y/n)")
    if answer=="n" or answer=="no" or answer=="No" or answer=="y" or answer=="yes" or answer=="Yes":
        c=1
if answer=="n" or answer=="no" or answer=="No":
    p=1
    while p==1:
        UsernameSet=input("What will your username be? ")
        fh = open("list.pkl", 'rb')
        try:
            Usernameport = pickle.load(fh)
        except EOFError:
            Usernameport = "."
        if not (UsernameSet in Usernameport):
            p=2
            fh.close()
    j=1
    while j==1:
        PasswordSet=input("What will your password be? ")
        fh = open("list.pkl", 'rb')
        try:
            Passwordport = pickle.load(fh)
        except EOFError:
            Passwordport = "."
        if not (PasswordSet in Passwordport):
            if  PasswordSet == UsernameSet:
                print("Your username and password cannot be the same")
            else:
                j=2
                print("You have created an account. please reload")
        fh.close()
    importthing = (UsernameSet,PasswordSet)
    fh = open("list.pkl", 'ab')
    pickle.dump(importthing, fh)
    fh.close()
if answer=="y" or answer=="yes" or answer=="Yes":
    p=1
    while p==1:
        Usernamething=input("Username: ")
        fh = open("list.pkl", 'rb')
        Usernamestuff = pickle.load(fh)
        if (Usernamething in Usernamestuff):
            p=2
        l=Usernamething
        fh.close()
    j=1
    while j==1:
        Passwordthing=input("Password: ")
        fh = open("list.pkl", 'rb')
        Passwordstuff = pickle.load(fh)
        if (Passwordthing in Passwordstuff):
            j=2
            print("you have logged in")
            sys.exit()
        fh.close()
  • 1
    Please see the following : https://stackoverflow.com/questions/20624682/pickle-dump-replaces-current-file-data – Charles Dupont May 04 '21 at 19:47
  • I said in my answer that when i decoded the pickle file for a test, It showed every account. It did not overwrite it. – amateurcoder67 May 04 '21 at 20:08
  • `if not answer=="y" or not answer=="yes" or not answer=="Yes" or not answer=="n" or not answer=="No" or not answer=="no":` This does not do what you want. It is always true. Please read https://en.wikipedia.org/wiki/De_Morgan%27s_laws and then https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true (you don't have the same problem as that, but the discussion is useful). – Karl Knechtel May 04 '21 at 20:15
  • it works in visual studio code where i am working, I do not know what you are talking about – amateurcoder67 May 04 '21 at 20:28

1 Answers1

1

Please review abarnert's answer at Pickle dump replaces current file data :

"when you append new dumps to the same file, you end up with a file made up of multiple separate values. If you only call load once, it's just going to load the first one. If you want to load all of them, you need to write code that does that. For example, you can load in a loop until EOFError."

def Load():
d = {}
with open('test.pkl', 'rb') as f:
    while True:
        try:
            a = pickle.load(f)
        except EOFError:
            break
        else:
            d.update(a)
# do stuff with d
Charles Dupont
  • 995
  • 3
  • 9