0

This is my code for Verifying User ID and password from a csv file.

CODE

import pandas as pd
login():
df=pd.read_csv("IDPASS.csv")
for i in range(len(df)):
    x=input("Enter User Name=")
    y=input("ENter The Password=")
    if (df.iloc[i]["ID"])==x:
        if str(df.iloc[i]["PASS"])==y:
            print("Succesfully Logined")
            break
        else:
            print("Invalid Password")
            print("Try Again!!")
            
    else:
        print("Invalid Username")
        print("Try Again!!")
login()

Output1:

Enter User Name=JEET1073
ENter The Password=str1234
Succesfully Logined

Here I have entered correct id password and the code is executed perfectly!!

Output2:

Enter User Name=dw
ENter The Password=wd
Invalid Username
Try Again!!
Enter User Name=JEET1073
ENter The Password=str1234
Invalid Username
Try Again!!
Enter User Name=JEET1073
ENter The Password=str1234
Invalid Username
Try Again!!
Enter User Name=JEET1073
ENter The Password=str1234
Invalid Username
Try Again!!
Enter User Name=JEET1073
ENter The Password=str1234
Invalid Username
Try Again!!

Here at first i have entered wrong id and password and then i was trying to enter the correct id and password but it was showing invalid username. any solutions will be appreciated,also i want a solution in which if a user enters wrong id password the code should run again asking id and password till the user id and password entered is not correct.Thanks in advance.

Jeet1073
  • 21
  • 4
  • Tangentially, the supine of "log in" is "logged in". – tripleee Sep 25 '20 at 06:21
  • Why are you looping over `len(df)`? If I am at all guessing what you are trying to accomplish, shouldn't you simply loop until the user enters an account name and password pair which exist anywhere in the dataframe? See also https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – tripleee Sep 25 '20 at 06:24

1 Answers1

1

After the clarification, this should be the functionality you are seeking.

First import pandas and read the csv:

import pandas as pd
df = pd.read_csv("IDPASS.csv")

We will now read the values stored in the csv and store them as a list. A list of users and a list of passwords. A good practice would be to do this outside of any loops.

csv_id_list = df['ID'].to_list()
csv_pass_list = df['PASS'].to_list()

If you don't know how long your loop will have to go on for (we don't know how many times the user will try the password) use a while loop. Initialize the initial state of the correct login variable.

correct_login = False

Now where the magic happens. We will start a while loop. While correct_login is false, we will keep asking the user for their username and password. IF the input by the user matches a list element, get the index for that list element, then start another IF statement. . This will break the while loop. ELSE they get an invalid password message, IF the input matches the list element at the correct index, print correct_msg and correct_login = True. Break while loop. ELSE correct_login is still false, user must try again.
This means that the user must input the correct username and the password it corresponds to, they cannot input the password of another user and log in.

while not correct_login:
    x = input("Enter User Name=")
    y = input("Enter The Password=")
    correct_msg = 'Successfully logged in.'
    incorrect_msg = 'Invalid Password.' + '\n' + 'Try Again.'
    if x in csv_id_list:
        x_index = csv_id_list.index(x)
        if y == csv_pass_list[x_index]:
            print(correct_msg)
            correct_login = True
        else:
            correct_login = False
            print(incorrect_msg)
    else:
        correct_login = False
        print(incorrect_msg)
rangeseeker
  • 395
  • 2
  • 9