Here's the code
login = open("login.txt", "a+")
passw = open("passw.txt", "a+")
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input())
if dec == 1:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
if log == login.read() and pas == passw.read():
print("You are logged in")
state = 1
login.close()
passw.close()
else:
print("Incorrect login or password")
state = 0
login.close()
passw.close()
elif dec == 2:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
login.write(log)
passw.write(pas)
print("You are registered")
state = 1
login.close()
passw.close()
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
when i tried to add another function between lines 41-42 its suddenly stopped working even reverting the code and running it now still doesn't work and now it constantly says "Incorrect login or password". I even cleared the files and registered fromt he beggining still the same result
I was logging in and was expecting to receive the message "You are logged in"
Here's the code
login = open("login.txt", "a+")
passw = open("passw.txt", "a+")
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input())
if dec == 1:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
if log == login.read() and pas == passw.read():
print("You are logged in")
state = 1
login.close()
passw.close()
else:
print("Incorrect login or password")
state = 0
login.close()
passw.close()
elif dec == 2:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
login.write(log)
passw.write(pas)
print("You are registered")
state = 1
login.close()
passw.close()
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
when i tried to add another function between lines 41-42 its suddenly stopped working even reverting the code and running it now still doesn't work and now it constantly says "Incorrect login or password". I even cleared the files and registered fromt he beggining still the same result
I was logging in and was expecting to receive the message "You are logged in"
Instead of opening files in a+ mode, you should use r+ mode to properly handle both reading and writing. Before performing a read operation, use seek(0) to reset the file pointer to the beginning; otherwise, read() may return an empty value. When comparing the read data, use .strip() to remove unnecessary spaces. Additionally, during the registration process, clearing the file content with truncate() before writing new entries will prevent old data from persisting. These changes will ensure that the login and registration functions work correctly without errors.
Here all recommendations that were mentioned in comments:
LOGIN_FILE = "login.txt"
PASS_FILE = "passw.txt"
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input("> "))
if dec == 1:
log = input("Enter your login: ")
pas = input("Enter your password: ")
with open(LOGIN_FILE, "r") as login:
login.seek(0)
# strip() removes `\n` (newline sybmbol) when it reads
login = login.read().strip()
with open(PASS_FILE, "r") as passw:
passw.seek(0)
passw = passw.read().strip()
if log == login and pas == passw:
print("You are logged in")
state = 1
else:
print("Incorrect login or password")
state = 0
elif dec == 2:
log = input("Enter your login: ")
pas = input("Enter your password: ")
with open(LOGIN_FILE, "w") as login:
login.truncate(0) # clear file content
login.write(log)
with open(PASS_FILE, "w") as passw:
passw.truncate(0)
passw.write(pas)
print("You are registered")
state = 1
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
Let's your third block of code (where actions with supasceret.txt
) be your "homework". And Try to use more often debugger (VS code, Pycharm, pdb, ipdb and etc). and google.
P.S. I'm not pro at python and at programming, and if somebody think that this code could be better, feel free do it.
a+
mode, the input pointer is positioned at the end of the file. So your.read()
call returns an empty string because the file is already at the end of its contents. – John Gordon Commented Jan 30 at 0:02with
statement. – Barmar Commented Jan 30 at 0:10