Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why isn't my function save_users(): working? How do I save these passwords to file?

from hashlib import sha256
import random, sys


def hash(string):
    '''Hashes a string'''

    return sha256(
        string.encode()).hexdigest() # when using hash() returns a hashed string


def save_users():
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close


print("\nWelcome")
signIn = input("Do you have an account? [Y/N]").upper()

if signIn == "N":
    print("Sign up :")

    username = input("New Username: ")
    password = hash(input("New Password: "))
    confirm = hash(input("ConfirmPassword: ")) == password
    print(password)

    if not confirm:
        print("Passwords do not match")

save_users()

"print (password)" is just there for me to test if it actually hashed the string as this is my first time doing anything like this. How would I save the password and username to an email, and similarly how would I authenticate the password and username?

Python 3.8.5

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace:

def save_users():
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close

With:

def save_users(username, password):
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close()

And call it with save_users(username, password) instead of save_users()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading