Python newbie and first time poster (but long time lurker) here so apologies for any formatting or protocol issues.
I’m trying to write a code that will prevent a duplicate user name from being written to file. I’ve tried the various solutions from previous similar questions on the forum but to no avail.
In my latest iteration so far, I’ve managed to get duplicates of the first key rejected, but duplicates of subsequent keys are still going through. I.e if my keys have the values ‘admin’ and ‘user'(which I’ve confirmed in my first print statement), x = admin is rightly being rejected but x = users is still progressing. Please help
My code:
alr_exists = {}
user = {}
with open('user.txt', 'r') as file:
for lines in file:
(keys, values) = lines.split()
keys = keys.replace(',', '')
alr_exists[keys] = keys
print(alr_exists[keys])
while True:
x = input("To register a new user, please enter a username:n").lower()
if x in alr_exists[keys]:
print(f'Sorry, the user name {x} has already been taken. Please try again.n')
continue
else:
y = input("Please enter a password:n").lower()
z = input("To confirm, please reenter your password:n").lower()
Cluelesspython is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.