0
votes
import ast

Account = {

    "username": "",
    "password": ""
}
firstQ = input("1:reg, 2:login, 3:quit")


def reg():
    Account["username"] = input("username: ")
    Account["password"] = input("password: ")

    AccountList = open("/Users/user/Desktop/acList.txt", "a")
    n = AccountList.write(str(Account) + "\n")
    AccountList.close()


def login():
    with open("/Users/user/Desktop/acList.txt", "r") as f:
        readAcc = f.read()
    realAcc = ast.literal_eval(readAcc)
    loginUsername = input("login with user: ")
    loginPassword = input("login with pass: ")
    if loginUsername in readAcc and readAcc[realAcc] == loginPassword:
        print("you are logged in as:" + realAcc["username"])


if firstQ == "1":
    reg()
elif firstQ == "2":
    login()
else:
    quit()

when trying to register multiple "account" I get a syntex error: invalid syntax. And when trying to log in to an account when you just have one account registered, I get a type error: string indices must be integers. Feel free to copy my code into your own editor as I may not have explained the error messages that well

1
Welcome to SO. Please include to the full traceback error.ewong
About which of the errors do you want to ask a question?mkrieger1

1 Answers

0
votes

Looks like the issue is in this code, you are trying to access a dictionary from the same dictionary. Here readAcc is a dict and this code will obviously raise an error: readAcc[readAcc]

if loginUsername in readAcc and readAcc[realAcc] == loginPassword:

you should probably consider readlines() instead of read() because the text file might have multiple lines of the dictionary.

import ast

Account = {}
firstQ = input("1:reg, 2:login, 3:quit")

def reg():
    Account["username"] = input("username: ")
    Account["password"] = input("password: ")

    AccountList = open("/Users/user/Desktop/acList.txt", "a")
    n = AccountList.write(str(Account) + "\n")
    AccountList.close()

def login():
    with open("/Users/user/Desktop/acList.txt", "r") as f:
        readAcc = f.readlines()

    loginUsername = input("login with user: ")
    loginPassword = input("login with pass: ")

    for dict_ in readAcc:
        realAcc = ast.literal_eval(dict_)

        if loginUsername and loginPassword in realAcc.values():
            print("you are logged in as:" + realAcc["username"])

if firstQ == "1":
    reg()
elif firstQ == "2":
    login()
else:
    quit()