So, Basically, I need my python code to read each line from an external file and remove everything after a comma. Then, it should check the user input with the new string.
Just like a verification process, where a user signs up and then the code checks whether the username is taken
But in the external file, user details are stored as: 'username,password'
Here is my code:
LogFile = 'accounts.txt'
invalidUserName = False
Uusername = input("Please enter your desired username: ")
while len(Uusername) < 3 or len(Uusername) > 16:
Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")
with open(LogFile) as f:
content = f.readlines()
contnt = [l.strip() for l in content if l.strip()]
passlist = []
userlist = []
for line in content:
try:
userlist.append(line.split(",")[0])
passlist.append(line.split(",")[1])
except IndexError:
pass
for username in userlist:
while username == Uusername:
Uusername = input(('Username is taken, please enter another one: '))
invalidUserName = True
if not invalidUserName:
password = input("Please enter your desired password: ")
while len(password) < 3 or len(password) > 16:
password = input("Password is not between the length of 3 and 16, please re-enter: ")
while password == username:
password = input("Password cannot be the same as the username, please re-enter: ")
VerPW = input("Please verify your password: ")
while VerPW != password:
VerPW = input("Passwords do not match, try again: ")
file = open ('accounts.txt','a')
file.write (username +','+password + "\n")
file.flush()
file.close
print ("Details stored successfully")
After line 5, I need the code to check usernames from the external file, without everything after the commas and ask the user to input again if a username already exists
list.txt:
emkay,123
alibaba,420
khanbaba,5647
naughty_princess,3242
whatthehack,657
and then:
logFile = "list.txt"
with open(logFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
# list to store the passwords
passList = []
# list to store the usernames
userList = []
for line in content:
userList.append(line.split(",")[0])
passList.append(line.split(",")[1])
print(userList)
print(passList)
user_inp = input("Enter a username: ")
for username in userList:
if username == user_inp:
print("Username: {}, already taken. Please try a different username".format(username))
OUTPUT:
['emkay', 'alibaba', 'khanbaba', 'naughty_princess', 'whatthehack']
['123', '420', '5647', '3242', '657']
Enter a username: naughty_princess
Username: naughty_princess, already taken. Please try a different username
EDIT:
If you want the loop to stop when a username is found. simply, break
away.
for username in userList:
if username == user_inp:
print("Username: {}, already taken. Please try a different username".format(username))
break
EDIT 2:
to apply that in the code you just posted,
if username == Uusername:
Uusername = ('Username is taken, please enter another one: ')
break # or use continue as it looks like you need it
EDIT 3:
Your code fixed, you need a boolean flag for the invalid users and proceed as follow:
LogFile = 'accounts.txt'
invalidUserName = False
Uusername = input("Please enter your desired username: ")
while len(Uusername) < 3 or len(Uusername) > 16:
Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")
with open(LogFile) as f:
content = f.readlines()
contnt = [l.strip() for l in content if l.strip()]
passlist = []
userlist = []
for line in content:
try:
userlist.append(line.split(",")[0])
passlist.append(line.split(",")[1])
except IndexError:
pass
print(userlist)
for username in userlist:
if username == Uusername:
print('Username is taken, please enter another one:')
invalidUserName = True
break
if not invalidUserName:
password = input("Please enter your desired password: ")
while len(password) < 3 or len(password) > 16:
password = input("Password is not between the length of 3 and 16, please re-enter: ")
while password == username:
password = input("Password cannot be the same as the username, please re-enter: ")
VerPW = input("Please verify your password: ")
while VerPW != password:
VerPW = input("Passwords do not match, try again: ")
file = open ('accounts.txt','a')
file.write (username +','+password + "\n")
file.flush()
file.close
print ("Details stored successfully")
EDIT 4:
LogFile = 'accounts.txt'
invalidUserName = False
while True:
Uusername = input("Please enter your desired username: ")
while len(Uusername) < 3 or len(Uusername) > 16:
Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")
with open(LogFile) as f:
content = f.readlines()
contnt = [l.strip() for l in content if l.strip()]
passlist = []
userlist = []
for line in content:
try:
userlist.append(line.split(",")[0])
passlist.append(line.split(",")[1])
except IndexError:
pass
print(userlist)
for username in userlist:
if username == Uusername:
print('Username is taken, please enter another one:')
invalidUserName = True
break
if not invalidUserName:
password = input("Please enter your desired password: ")
while len(password) < 3 or len(password) > 16:
password = input("Password is not between the length of 3 and 16, please re-enter: ")
while password == username:
password = input("Password cannot be the same as the username, please re-enter: ")
VerPW = input("Please verify your password: ")
while VerPW != password:
VerPW = input("Passwords do not match, try again: ")
file = open ('accounts.txt','a')
file.write (username +','+password + "\n")
file.flush()
file.close
print ("Details stored successfully")
exit_now = input("Press 1 to exit")
if '1' in exit_now:
break