I'm trying to create a basic Sign-up and Sign-in system in python using text files. I also want the program to run in one go, meaning I want the user to be able to signup, and then proceed to log in right away, instead of having them go back and run the code again. The code that I have written though, can't do that for some reason. I've added "signin()" at the end of the sign up function to allow the users to proceed to sign in after a successful registration, but doing so throws an error. Instead I have to run the code again for the new signup to be recognized.
Here's the code:
def signin():
q = 0
user = input('Username: ')
password = input('Password: ')
with open('username.txt', mode='r')as names:
for i in names:
foundname = names.readlines()
with open('password.txt', mode='r')as passwords:
for j in passwords:
foundpass = passwords.readlines()
if user + "\n" in foundname: # ReadLines() adds newlines
user_num = foundname.index(user + "\n")
for i in range (0,user_num):
if password + "\n" == foundpass[i]:
print("Login Successful.")
print("Incorrect Password. Try Again.")
signin()
else:
print("Username not found. Try again")
signin()
def signup():
with open('username.txt', mode='a')as names:
print("Please provide the following details to sign up")
name = str(input("Username: "))
names.write(f"{name}\n")
with open('password.txt', mode='a')as passwords:
password = (input("Password: "))
passwords.write(f'{password}\n')
print("signup successful. You can sign-In Now.")
signin()
signup()
Here's the output and the error I keep getting:
Please provide the following details to sign up
Username: abcdef
Password: password1
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Traceback (most recent call last):
File "D:/student.py", line 34, in <module>
signup()
File "D:/student.py", line 33, in signup
signin()
File "D:student.py", line 14, in signin
if password + "\n" == foundpass[i]:
IndexError: list index out of range
Process finished with exit code 1
But when I run the code again, the credentials I used to sign up the last time seem to work.
Please provide the following details to sign up
Username: jkl
Password: mno
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Login Successful.
Thanks a lot in advance!