Home > Software engineering >  Check if there is a duplicate in csv file
Check if there is a duplicate in csv file

Time:01-04

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

CodePudding user response:

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')

CodePudding user response:

The break goes out the first for but it is still in the while loop.

CodePudding user response:

infinite loop because you use

while true:
  ....

You may initial a variable

goodtogo=False

Above the while loop and when the before break does it job, you may

goodtogo=True

Then after for loop, check if

if goodtogo:
   break

again.

  • Related