简体   繁体   中英

How can we check and re ask the user to enter username if it is already taken from csv file

def datafirstrow():
with open("data.csv", "w") as obj:
    fileobj = csv.writer(obj)
    fileobj.writerow(["Username", "Password", "First Name","Last Name","UserID","UPI ID","Bank Details","Mobile Info"])

datafirstrow()

Above are the columns of my csv file.

def newUser(userid):
    with open("data.csv", "a") as obj:
        fileobj = csv.writer(obj)
        while(True):
            username = check(input("Enter username: "))
            password = input("Enter password: ")
            pwd = (hashlib.sha256(password.encode('utf-8')).hexdigest())
            First = input("Enter First Name: ")
            Last = input("Enter Last Name: ")
            Upi = input("Enter UPI ID: ")
            print("Enter Bank Details: \n")
            bankDetails = ""
            BankNumber = str(input("Please enter Account Number \n"))
            bankDetails += ('Bank Account Number: {}\n'.format(BankNumber))
            BankName = str(input("Enter the Bank Name: \n"))
            bankDetails += ('Bank Name: {}\n'.format(BankName))
            BankHolderName = str(input("Enter Account Holder Name \n"))
            bankDetails += ('Account Holder Name: {}\n'.format(BankHolderName))
            IFSC = str(input("Enter IFSC Code"))
            bankDetails += ('Branch IFSC Code: {}\n'.format(IFSC))
            mobile = input("Enter your Contact Number: ")
            record = [username, pwd,First,Last,userid,Upi,bankDetails,mobile]
            fileobj.writerow(record)
            break 
        obj.close()

Above is the function I am using to add new data to the table

def check(username):
    with open("data.csv", "r") as obj:
        fileobj2 = csv.reader(obj)
        for i in fileobj2:
            next(fileobj2)
            if i[0] == username:
                print("Username is taken \n Please input a unique username \n")
                username = input("Enter Username: ")
                checkusername(username)
            elif i[0] != username:
                break
        obj.close()
    return username

check(input("enter username"))

This function checks if the username is taken or not But it checks the username once and then if I enter the same username again it returns me the same username

What I want is it to check the username everytime

I changed your code a bit (I have a comment where I change the code) I hope this will work. If not let me know in the comment.

import csv
import hashlib

def datafirstrow():
    with open("data.csv", "w") as obj:
        fileobj = csv.writer(obj)
        fileobj.writerow(["Username", "Password", "First Name","Last Name","UserID","UPI ID","Bank Details","Mobile Info"])

datafirstrow()


def newUser(userid):
    with open("data.csv", "a") as obj:
        fileobj = csv.writer(obj)
        username = None #changed
        while username is None:  #changed
            username = check(input("Enter username: "))  #changed
            
        password = input("Enter password: ")
        pwd = (hashlib.sha256(password.encode('utf-8')).hexdigest())
        First = input("Enter First Name: ")
        Last = input("Enter Last Name: ")
        Upi = input("Enter UPI ID: ")
        print("Enter Bank Details: \n")
        bankDetails = ""
        BankNumber = str(input("Please enter Account Number \n"))
        bankDetails += ('Bank Account Number: {}\n'.format(BankNumber))
        BankName = str(input("Enter the Bank Name: \n"))
        bankDetails += ('Bank Name: {}\n'.format(BankName))
        BankHolderName = str(input("Enter Account Holder Name \n"))
        bankDetails += ('Account Holder Name: {}\n'.format(BankHolderName))
        IFSC = str(input("Enter IFSC Code"))
        bankDetails += ('Branch IFSC Code: {}\n'.format(IFSC))
        mobile = input("Enter your Contact Number: ")
        record = [username, pwd,First,Last,userid,Upi,bankDetails,mobile]
        fileobj.writerow(record)

        obj.close()



def check(username): #changed
    with open("data.csv", "r") as obj:
        fileobj2 = csv.reader(obj)
        if username in map(lambda e:e[0],list(fileobj2)[1:]):
            print("Username already exists.")
            return None
    obj.close()
    return username
result = None
while result is None:
    result = check(input("enter username"))
   
print(result)

Thank you everyone for helping I got the solution-

I changed my check(username) function to read every username column of the data file and make a list and then check if the username is in the list or not

Heres the code-

def check(username): #changed
    with open("data.csv", "r") as obj:
        fileobj2 = csv.DictReader(obj)
        usernames = []
        for col in fileobj2:
            usernames.append(col['Username'])
        if username not in usernames:
            return username
        print("Username already exists.")
        username = input("Enter username")
        return check(username)
        
    obj.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM