Why is it skipping lines in the txt in my code?

76 Views Asked by At

I am trying to make a password manager. I want it to ask the user what website they saved it for, and then give it the password and the username for it. Instead, it just skips through the text and doesn't show the username, but the password of the first 2 lines, not the username and password of the account.

This is my code:

def view_passwords():
    try:
        website = simpledialog.askstring("Enter Website", "Enter the website for the account:")
        with open(passwordsfile, 'r') as f:
            passwords = f.read()
            # Search for the account based on the provided website
            if website in passwords:
                # Split the passwords string based on the separator "|"
                entries = passwords.split("|")
                for entry in entries:
                    if "Website or app" in entry:
                        # Extract the username and password
                        username = entries[entries.index(entry) - 1].strip()
                        password = entries[entries.index(entry) + 1].strip()
                        messagebox.showinfo("Account Info", f"Website: {website}\n{username}\n{password}")
                        break
                else:
                    messagebox.showinfo("No Account", f"No account found for {website}.")
            else:
                messagebox.showinfo("No Account", f"No account found for {website}.")
    except FileNotFoundError:
        messagebox.showinfo("No Passwords", "No passwords found.")

This is my text file:

Username: hdhdh| Password: hshsh| Website or app: snsn
Username: dhhd| Password: shahhs| Website or app: [Z!+M7gt[_uw
Username: aaa| Password: $LCN}|d1HsrR| Website or app: ok

When I run it, this happens:

enter image description here

Can you guys help me?

0

There are 0 best solutions below