Python Forum
Quick Question about Dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Quick Question about Dictionaries (/thread-37077.html)



Quick Question about Dictionaries - Extra - Apr-29-2022

Hello,

I'm trying to make a simple login page through the terminal and I decided to use a dictionary to store the user's name and password.

As show in my code here (Ya I realize now, I probably don't need the whole function housing the dict):
def StandardUsers():
    users = {
        "Bob" : 1234,
        "Jim" : 5678,
        "Roger" : 9101,
        "Frank" : 8877
    }
    return users

def SuperUsers():
    admins = {
        "Joe" : 0000,
        "Randall" : 1111
    }
    return admins
(Ex: "Bob":1234 Where "Bob" is the user's name and 1234 is the password)

And I have an input statement that takes in the user's name and password as shown here:

def login():
    print('=============================')
    print('= Login =')
    print('=============================')
    user = input("Enter your name: ")
    password = input("Enter your password: ")
    validate() #Go to validate function
And this is my validate function that I have right now:
def validate(user):
    #if StandardUsers enters in correct name and password:
        MainMenu()

    #if SuperUsers enters in the correct name and password:
        AdminMenu()
        
    else:
        print('You are not registered to use this program')
        login()



My question is: how do I validate the user's name and password in an if statement so they can get to the menu? (How do I check to make sure the key, value pairs in the dict match, and apply them to my validate function)

Thanks in advance.


RE: Quick Question about Dictionaries - deanhystad - Apr-29-2022

if StandardUsers().get(user) == password: should suffice. You will need to pass the username and password to the validate function. You should also give some feedback if validation fails.


RE: Quick Question about Dictionaries - Extra - Apr-29-2022

(Apr-29-2022, 05:32 PM)deanhystad Wrote: if StandardUsers().get(user) == password: should suffice. You will need to pass the username and password to the validate function. You should also give some feedback if validation fails.

So I passed in the user and password to the validate Function but when I try to log in (I tried Bob, (hit enter) then 1234 (enter again)), it won't let me (it just prints out my else statement: You are not registered to use this program)

def validate(user, password):
    if StandardUsers().get(user) == password:
        MainMenu()#go to mainMenu
        
    else:
        print('You are not registered to use this program')
        login()
Any ideas on why it's not working?


RE: Quick Question about Dictionaries - Extra - Apr-29-2022

(Apr-29-2022, 05:48 PM)Extra Wrote:
(Apr-29-2022, 05:32 PM)deanhystad Wrote: if StandardUsers().get(user) == password: should suffice. You will need to pass the username and password to the validate function. You should also give some feedback if validation fails.

So I passed in the user and password to the validate Function but when I try to log in (I tried Bob, (hit enter) then 1234 (enter again)), it won't let me (it just prints out my else statement: You are not registered to use this program)

def validate(user, password):
    if StandardUsers().get(user) == password:
        MainMenu()#go to mainMenu
        
    else:
        print('You are not registered to use this program')
        login()
Any ideas on why it's not working?

Never mind.
I changed my password input statement to take in an int instead of a string
 password = int(input("Enter your password: "))
It now works.

And thanks for the help.


RE: Quick Question about Dictionaries - bowlofred - Apr-29-2022

Never mind.


RE: Quick Question about Dictionaries - deanhystad - Apr-29-2022

You should not use recursion for this. Instead of having validate call login if validation fails, login should call validation in a loop until the validation is successful.
def validate(user, password):
    if StandardUsers().get(user) == password:
        MainMenu()#go to mainMenu
        return True
    return False

def login():
    while True:
        user = input("Enter your name: ")
        password = input("Enter your password: ")
        if validate(user, password):
            break
        else:
            print('You are not registered to use this program')



RE: Quick Question about Dictionaries - Extra - Apr-29-2022

(Apr-29-2022, 06:51 PM)deanhystad Wrote: You should not use recursion for this. Instead of having validate call login if validation fails, login should call validation in a loop until the validation is successful.
def validate(user, password):
    if StandardUsers().get(user) == password:
        MainMenu()#go to mainMenu
        return True
    return False

def login():
    while True:
        user = input("Enter your name: ")
        password = input("Enter your password: ")
        if validate(user, password):
            break
        else:
            print('You are not registered to use this program')

Thanks for the advice.