Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if there is a duplicate in csv file

Why does the following code give an infinite loop and not correctly check if there is a same element as the username input in the file using the csv module in Python, and when i try to check in input existing users username and password it prints ‘this username is taken’ and ‘succseful registration’ together. i want to Write a function that allows a user to register by inputting a username and password, and stores the values in a CSV file. The function should check if the username is already taken and whether the password contains only numbers. If the registration is successful, the function should print a success message. If the username is already taken or the password is invalid, the function should print an error message and allow the user to try again.

def register():
    try:
        with open('users.csv','x') as f:
            pass
    except:
        pass
    finally:
        with open('users.csv','r+',newline='') as f:
            while True:
                lines = csv.reader(f)
                username=input('type your username: ')
                password=input('type your password (must contain only numbers): ')
                for i in lines:
                    if username not in i[0]:
                        if password.isdigit():
                            writer = csv.writer(f)
                            writer.writerows([[username,password]])
                            print('successful registration')
                            break
                        else:
                            print('password must contain only numbers')
                    else:
                        print('this username is already taken')

this is elemnts in csv file:

user,0809
user2,5677

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming this is the whole function, you can just use return instead of break:

def register():
    try:
        with open('users.csv','x') as f:
            pass
    except:
        pass
    finally:
        with open('users.csv','r+',newline='') as f:
            while True:
                lines = csv.reader(f)
                username=input('type your username: ')
                password=input('type your password (must contain only numbers): ')
                for i in lines:
                    if username not in i[0]:
                        if password.isdigit():
                            writer = csv.writer(f)
                            writer.writerows([[username,password]])
                            print('successful registration')
                            return
                        else:
                            print('password must contain only numbers')
                    else:
                        print('this username is already taken')

But this doesn’t work correctly as for the usernames. Here’s what I suggest:

with open('users.csv','r+',newline='') as f:
    user_passwords = dict(csv.reader(f))
    while True:
        username=input('type your username: ')
        password=input('type your password (must contain only numbers): ')
        if username not in user_passwords:
            if password.isdigit():
                writer = csv.writer(f)
                writer.writerows([[username,password]])
                print('successful registration')
                return
            else:
                print('password must contain only numbers')
        else:
            print('this username is already taken')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading