Python Forum
Help with Account login in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Account login in Python
#1
Photo 
Hi! I just start learning Python and I wanted to push myself and try to make a little program that will verify the user position, userid and password before loading the main menu. The account credentials are store in a table in database. Below is a quick description of what I want the program to perform.
  • Ask the user to enter his role one the company
    Ask the user to enter his userid
    Ask the user enter his password

    The program should look in the database and find the record, compare the record to the credentials entered.
    If credentials is correct load the main menu otherwise prompt the user to enter the correct credentials.
    After three trials the user account should be locked.

    My problem with the current code is that the program go through the first loop only and will never go through the validation of the credentials.

    Here is how my database table look like.

    userid password role
    PD001 pdmanager1 Product Manager
    RET001 retailer1 Retailer
    SMGR001 seniormgr1 Senior Manager
    SM001 smanager1 Site Manager

    Below is a copy of my code.


import sqlite3

userid = ""
password = ""
role = ""

connect_fworlddb = sqlite3.connect('fworld.db')
myCursor = connect_fworlddb.cursor()
myCursor.execute('SELECT * FROM account_tbl WHERE userid = ? and password = ? and role = ?',(userid,password,role))
allusers = myCursor.fetchall()

for chances in range(3,0,-1): #count number of tries

    xrole = input("Enter your Role: ")
    xuserid = input("Enter UserID: ")
    xpassword = input("Enter Password: ")

    for user in allusers:   #looping in allusers list to find user

        for  credential in user:     #looping on one user tuple to validate credentails if not found move to next tuple.

            if credential[0] == xuserid and credential[1] == xpassword and credential[2] == xrole: #validation of credentials
                print("You have Successfully logged in Fishing World")
                break
            else:
                print("Either your role, username or password is wrong.")
                print("You have" + " " + str(chances) + " " + "chances left.")
                print("Please Try Again!")
if chances == 1:
    print("User" + " " + xuserid + " " + "account has been locked.")
    print("Please contact the system Administrator on ext. 5408")
    exit
Reply
#2
If I'm understanding correctly it works on the following loop
for chances in range(3,0,-1): #count number of tries
but not
for user in allusers:   #looping in allusers list to find user
print the contents of allusers to see what it contains.

I think you need to make your mind up to either check the database for a specific record or get all records and check amongst the result for a record.
Reply
#3
Correct the first loop "Chances" that count the number of login attempts works fine. But it seems like it will never enter the second loop and nothing reach the IF statement.

I tried print(allusers) and it return an empty []"list"
Reply
#4
That's why the for loop does nothing, now look at why your database query returns an empty list.
what do you expect it to return with the query you currently have?
Reply
#5
as per my query i want it to return userid, password and role and I want to loop through this result and compare it to user entry
Reply
#6
Your query is asking to return all from account_tbl where userid, password and role are an empty strings, which is going to return an empty list.
Reply
#7
The code seems to work fine however it is not really practical... is the For Loop a good choice or a While loop will be better?

for chances in range(3,0,-1):

    xrole = input("Enter your Role: ")
    xuserid = input("Enter your UserID: ")
    xpassword = input("Enter your Password: ")

    for user in allusers:
        print(user)
        if user[2]==xrole and  user[0]==xuserid and user[1]==xpassword in user:
            print("Found")
            break
        else:
            print("Not Found")
if chances == 1:
    print("User" + " " + xuserid + " " + "account has been locked.")
    print("Please contact the system Administrator on ext. 5408")
Reply


Forum Jump:

User Panel Messages

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