Reputation: 25
This is a school project that I'm trying to do, we have to create a program that asks the user if they have already had an account or not. If not they would have to enter a username, the program will have to check whether the username is already used or not.
import csv
with open('accountdatabase.csv')as csvfile:
datareader = csv.reader(csvfile, delimiter=',')
for row in datareader:
print("###################################")
accountq = input("Do You Have An Account? Y/N: ")
create = "pending"
while create != "completed":
if accountq == "N":
print("Please Create An Account:")
print("")
username = input("Please Enter Your Username: ")
password = input("Please Enter A Password: ")
if row[0] == username:
print("Sorry Username Is Already Taken")
else:
print("Username Accepted")
create = "completed"
break
However, when writing the code, I came an across a logic error where whenever I add a new username it only checks the last username on the row, meaning the previous one is not being checked, this leads to the user being able to create a new account with the same usernames.
I still struggle calling in specific data from a specific Period and row no.
This is how I structure my data in the .csv file:
Upvotes: 2
Views: 2945
Reputation: 7353
You can do this easily using pandas
library. The solution will be fast and smaller.
Note: you may have to install pandas
if don't already have it.
pip install -U pandas
import pandas as pd
df = pd.read_csv('accountdatabase.csv', sep=',')
# Your trial username
trial_username = 'bob12'
# The username column is called 'username' in the CSV file
if trial_username in df['username'].tolist():
print('Username NOT available.')
else:
print('Username available.')
We assume that once you read in the file using pandas.read_csv
method, you have a dataframe df
as follows.
df = pd.DataFrame({'username': ['bob12', 'rickrock', 'jango.tango', 'captain.america'], 'password': None})
print(df)
Output:
username password
0 bob12 None
1 rickrock None
2 jango.tango None
3 captain.america None
Now you want to test if a certain username is already taken
or available
. Here is the code for that.
trial_username = 'bob12'
if trial_username in df['username'].tolist():
print('Username NOT available.')
else:
print('Username available.')
Result:
Username NOT available.
If SignUp:
1. Check username availability.
1. If username is available: let user provide a password.
1. Save username and password to file.
If SignIn:
1. Check match for both username and password.
1. If match found: Credentials are VALID, else INVALID.
Assume this is your data.
df = pd.DataFrame({'username': ['bob12', 'rickrock', 'jango.tango', 'captain.america'], 'password': None})
df['password'] = ['passw0rd']*df.shape[0]
print(df)
Dummy Data:
username password
0 bob12 passw0rd
1 rickrock passw0rd
2 jango.tango passw0rd
3 captain.america passw0rd
Check username and password validity for an existing user:
user_username = 'bob12'
user_password = 'passw0rd'
valid_user = df['password'].loc[df['username'] == user_username] == user_password
print(valid_user)
Upvotes: 1
Reputation: 956
Just keep a list of usernames when you parse the csv file:
import csv
usernames = []
with open('accountdatabase.csv')as csvfile:
datareader = csv.reader(csvfile, delimiter=',')
for row in datareader:
usernames.append(row[0])
.....
if username in usernames:
print("Sorry Username Is Already Taken")
This approach keeps the names in memory. This isn't exactly scalable, as it uses a ton of memory (O(N)
space). You could also reread the csv and if you come across the username quit, but that would take O(N)
time. Neither approach is really scalable. But either solution will work for this project. I'd recommend keeping them in memory since this is a small project, which is what the code above does.
Upvotes: 1
Reputation: 31
Kindly rewrite the code :
-rest of the code-
for row in dataheader: if row[0] == username
-rest of the code-
Hope It works
Upvotes: 1