admin管理员组

文章数量:1315778

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"

Share Improve this question edited Jan 30 at 0:15 InSync 10.9k4 gold badges17 silver badges56 bronze badges asked Jan 30 at 0:01 KosteQKosteQ 111 silver badge1 bronze badge 6
  • 3 When you open the files using 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:02
  • Don't tell us "I tried to add another function between lines 41-42". First, there are no line numbers. Second, we don't know what you tried to add. Show us the script with the problem, not the script you tried to modify. – Barmar Commented Jan 30 at 0:08
  • 1 Aside: Learn to use context managers with the with statement. – Barmar Commented Jan 30 at 0:10
  • Wait until the user tells you whether they're logging in or registering before opening the files. If they're logging in, open the files for reading. If they're registering, open them for writing. – Barmar Commented Jan 30 at 0:11
  • 1 In neither case should you open them in append mode. The files only contain one username and password, there's no reason to add to them. – Barmar Commented Jan 30 at 0:12
 |  Show 1 more comment

2 Answers 2

Reset to default 1

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.

本文标签: