[Python] Python need to read a file line to detect username and password

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I need to make a banking program In python and I have trouble trying to get the program to read the file for usernames and passwords whilst only reading from one line to another. I would need it so that after you input your username and password, it would read the text file but line by line and would only detect the username and password if they were both on the same line of text.

    accounts = {}

    def login():

    f = open("ANZData.txt", "a")
    y = 0
    count = 0

    while y != 1:
    print("whats your username? ")
    loginuser = input()

    print("whats your password? ")
    loginpass = input()

    print("whats your PIN ")
    loginpass = input()

    # Open the file in read mode
    with open('ANZData.txt', 'r') as file:
    # Read each line in the file
    for line in file:
    #CODE NEEDED HERE




    def register(accounts):

    print("what is your username?")
    user = input("enter username:")

    print("what is your Password? ")
    passw = input("enter password: ")

    print("please provide us a pin number to use")
    PIN = input("enter PIN: ")

    with open('ANZData.txt', 'a') as f:
    f.write("name: " + user + " Password: " + passw + " PIN: " + PIN + '\n')


    with open('ANZData.txt', 'r') as file:
    for line in file:
    key, value = line.strip().split(':', 1)
    accounts[key.strip()] = value.strip()

    print(accounts)



    #Main code starts here:
    #--------------------------------------------------------------------------------------------#

    x=1

    while x != 0:

    print(accounts)
    print("Would you like to login or register?")
    logintoken = input("")

    if logintoken == "enter":
    registr = register(accounts)
    elif logintoken == "login":
    x=0
    logi = login()
    else:
    print("try again")

    x=1

    while x != 0:
    print(accounts)
    print("\n Do you want to withdraw, deposit or check balance?")
    decision = input("")

    if decision.upper == "Balance":
    print("placeholder")


    I've tried writting:

    if loginuser and loginpass in file.readlines():


    however that would lead to errors and I don't know where else to go.

    Continue reading...

Compartilhe esta Página