0
import linecache
print("Welcome to the online safety quiz!") 



registereduser = input("Are you a registered user? Enter Y or N: ") 

registereduser = registereduser.upper() 


if registereduser == "Y":
    username = input("What is your school username? ")
    stored_username = linecache.getline("{0}.csv".format(username), 4)
    print (stored_username)

    if username == stored_username:
        stored_password = linecache.getline("{0}.csv".format(username), 5)
        stored_fullname = linecache.getline("{0}.csv".format(username), 1)
        print("Hello {0} ").format(stored_fullname)
        password = input("What is your password? ")
        import hashlib, uuid
        salt = "87e6781077c7420cbc160853b62693f3"
        hashed_password = hashlib.sha512(password.encode('utf-8') + salt.encode('utf-8')).hexdigest()
    if stored_password == hashed_password:

        print("You have logged in successfully")
    else:
        print("ERROR Your details do not match the one in our database please retry or register again ") 

This is the error i get when i run the code. Traceback (most recent call last): File "C:\Users\joel\python", line 25, in if stored_password == hashed_password: NameError: name 'stored_password' is not defined

Please bear in mind i already have data files stored in the same folder which the program retrieves data from...

3 Answers 3

2

The problem is that stored_password is only declared if username == stored_username. Fix this by declaring it beforehand with a default value.

stored_password = ''
if username == stored_username:
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Using a string might not be best as conceivably a user could enter it as their username...
0

Stored_password is defined locally within the if statement. you need to add stored_password = None on line 16

2 Comments

If statements do not have their own level of scope.
I misspoke, What I mean is, if the username entered does not match the username_stored stored_password will not be defined, becasue Stored_password is only defined in the conditional... (forgive me)
-1

Your indentation is off. With the current indention its on the same level as if the username == stored_username.

If you indent your if statement it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.