0

the following code is a Password Manager which shows up with exit code 0 which means it has no errors,but its supposed to as you will see allow external input from the user as a password manager should....help

from cryptography.fernet import Fernet

class PasswordManager:

    def __init__(self) -> None:
        self.key = None
        self.password_file = None
        self.password_dict = {"site"}

    def create_key(self, path):
        self.key = Fernet.generate_key()
        with open(path, 'wb') as f:
            f.write(self.key)

    def load_key(self, path):
        with open(path, 'rb') as f:
            self.key = f.read()

    def create_password_file(self, path, initial_values=None):
        self.password_file = path

        if initial_values is not None:
            for key, value in initial_values.items():
                self.add_password(key, value) 

    def load_password_file(self, path):
        self.password_file = path

        with open(path, 'r') as f:
            for line in f:
                site, encrypted = line.split(":")
                self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()

    def add_password(self, site, password):
        self.password_dict[site] = password

        if self.password_file is not None:
            with open(self.password_file, 'a+') as f:
                encrypted = Fernet(self.key).encrypted(password.encode())
                f.write(site + ":" + encrypted.decode() + "\n")

    def get_password(self, site):
        return self.password_dict[site]

def main():
    password = {
        "email": "1234567",
        "facebook": "fbpassword",
        "tik tok": "tikinbomb323",
        "instaagram": "instapinsta678!"
    }

    pm = PasswordManager()

    print("""What do you want do?
    (1) Create a new key
    (2) Load an existing key
    (3) Create new password file
    (4) Load existing password file
    (5) Add a new password
    (6) Get a password
    (q) Quit
    """)

    done = False

    while not done:

        choice = input("Enter your choice: ")
        if choice == "1":
            path = input("Enter path: ")
            pm.create_key(path)
        elif choice == "2":
            path = input("Enter path: ")
            pm.load_key(path)
        elif choice == "3":
            path = input("Enter path: ")
            pm.create_password_file(path, password)
        elif choice == "4":
            path = input("Enter path: ")
            pm.load_password_file(path)
        elif choice == "5":
            site = input("Enter the site: ")
            password = input("Enter the password: ")
            pm.add_password(site, password)
        elif choice == "6":
            site = input("What site do you want: ")
            print(f"Password {site} is {pm.get_password(site)}")
        elif choice == "q":
            done = True
            print("Good bye and have a nice day")
        else:
            print("Invlaide choice, please try again")


    if __name__ == "__main__":
        main()

I tried reviewing youtube reddit but no progress just....exit code 0

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • 2
    is `if __name__ == "__main__":` inside of `main()` ? – Derek O Feb 08 '23 at 04:07
  • BTW "exit code 0" means everything was fine. – Klaus D. Feb 08 '23 at 04:12
  • Welcome to Stack Overflow. I can't understand the question. "the following code is a Password Manager which shows up with exit code 0 which means it has no errors" - so, in other words, when you try to use the code, nothing wrong happens. What needs to be fixed? – Karl Knechtel Feb 08 '23 at 04:16
  • Ah, I see. It seems like you were expecting to see something *else* happen, but you *only* see this message (meaning, the program exited immediately). This is a typo: your `if __name__ == '__main__'` test is indented inside of `main`, so it cannot run. Just in case you do not understand why it is a typo, I will provide an appropriate duplicate link. – Karl Knechtel Feb 08 '23 at 04:17
  • In the future, [please try](https://meta.stackoverflow.com/questions/261592) to [find simple problems](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) like this yourself, for example by reading the code carefully, or working through a checklist of common problems you had before, or trying to create a [mre]. – Karl Knechtel Feb 08 '23 at 04:20

0 Answers0