Python Forum
Quick Question about Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick Question about Dictionaries
#1
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.
Reply
#2
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.
Reply
#3
(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?
Reply
#4
(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.
Reply
#5
Never mind.
Reply
#6
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')
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Question about working with dictionaries Ashcora 13 2,080 Dec-27-2022, 09:09 PM
Last Post: Ashcora
  Quick question/help regarding my variable Extra 5 1,550 May-06-2022, 12:01 AM
Last Post: Extra
  quick question/excel+python zarize 1 2,269 Dec-06-2019, 05:38 PM
Last Post: Larz60+
  A quick question teczone 4 3,085 Sep-06-2018, 03:44 PM
Last Post: snippsat
  Completely new to coding - quick question Oster22 1 2,702 Jun-19-2018, 08:42 PM
Last Post: Larz60+
  Quick help! Ellisrb 2 2,763 May-02-2018, 11:21 AM
Last Post: ThiefOfTime
  Quick Lists Question (Count Occurences) EwH006 9 8,044 Nov-16-2016, 04:33 PM
Last Post: Larz60+
  quick question about deleting an object from memory LavaCreeperKing 5 5,838 Nov-12-2016, 04:05 PM
Last Post: LavaCreeperKing
  quick way to convert in both 2 and 3 Skaperen 10 8,769 Nov-03-2016, 04:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020