oiestin oiestin - 2 months ago 38
Python Question

How do I make a password program in Python 3.4?

I am currently username and password was equivalent to one found in the code. This is what I have so far:

import os

#Must Access this to continue.
def main():
while True:
UserName = input ("Enter Username: ")
PassWord = input ("Enter Password: ")

if UserName == Bob and PassWord == rainbow123:
time.sleep(1)
print ("Login successful!")
logged()

else:
print ("Password did not match!")

def logged():
time.sleep(1)
print ("Welcome to ----")

main()


After I run the code, I receive this error:

Traceback (most recent call last):
File "C:\Users\Austin\Desktop\oysterDev\oysterDev.py", line 23, in <module>
main()
File "C:\Users\Austin\Desktop\oysterDev\oysterDev.py", line 11, in main
if UserName == Bob and PassWord == rainbow123:
NameError: name 'Bob' is not defined


Does anyone know what I am doing wrong? Or where I could find some code that works like this for Python 3.4? Thanks!

Answer

Right now your checking for a correct password and user with:

if UserName == Bob and PassWord == rainbow123:

Without quotes python expects that bob and rainbow123 are defined variables. Since they're not defined it throws a NameError.

Just enclose those values in quotes:

import os

#Must Access this to continue.
def main():
    while True:
        UserName = input ("Enter Username: ")
        PassWord = input ("Enter Password: ")

        if UserName == 'Bob' and PassWord == 'rainbow123':
            time.sleep(1)
            print ("Login successful!")
            logged()

        else:
            print ("Password did not match!")

def logged():
    time.sleep(1)
    print ("Welcome to ----")

main()