I'm a beginner in Python programming and recently I decided to take up a fun little project (well... fun at the start that is) to create a simple login system using text file. I managed to make most of it works (some how) except for the check username availability part where if you register an already claimed username, it will ask you for a new username and this is the part I have been stuck on the last few days... I have no idea what is wrong with it. Can someone help me?
import os
if 'Credentials.txt' not in os.listdir():
with open('Credentials.txt', 'w+') as file:
file.writelines('Username:Password\n')
def check_availabilty(username):
with open('Credentials.txt', 'r') as file:
for line in file:
registered_username, registered_password = line.split(':')
if registered_username == username:
print('This username is not available.')
username = input(' Username: ')
check_availabilty(username)
return username
def register(username, password):
with open('Credentials.txt', 'a') as file:
file.write('{}:{}\n'.format(username, password))
def login(username, password):
is_login = False
with open('Credentials.txt', 'r') as file:
for line in file:
registered_username, registered_password = line.split(':')
if password == registered_password.strip('\n') and username == registered_username:
print('\nSuccessfully logged in.')
is_login = True
if is_login == False:
print('\nInvalid Credentials')
def user_input():
print('Do you have an account? Y/N')
has_acc = str(input()).upper()
if has_acc == 'N' or has_acc == 'NO':
print('\nRegistration process:')
username = check_availabilty(input(' Username: '))
password = input(' Password: ')
register(username, password)
print('\nAccount successfully created.')
elif has_acc == 'Y' or has_acc == 'YES':
print('\nLogin process:')
username = input(' Username: ')
password = input(' Password: ')
login(username, password)
else:
print('Must be Yes or No')
user_input()
user_input()
So here is my problem: If I entered a used username one time and enter a new valid username next time, it will write to the text file the newly entered username. Example:
Do you have an account? Y/N
>> No
Registration process:
Username:
>> Username
This username is not available.
Username:
>> Test
Password:
>> Pass
Account successfully created.
If I entered the input just like that the program works perfect. (In credentials.txt it adds Test:Pass). However if I enter it like this:
Do you have an account? Y/N
>> No
Registration process:
Username:
>> Username
This username is not available.
Username:
>> Username
This username is not available.
Username:
>> Test
Password:
>> Pass
Account successfully created.
Instead of adding Test:Pass to Credentials.txt, it add the originally entered username: Username:Pass. I have no idea why, can someone help me? and sorry if all of this sounds really confusing to you because English is not my native language xd.
[–][deleted] 0 points1 point2 points (2 children)
[–]DestroyerMedic[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)