简体   繁体   English

帐户创建程序未正确读取文件

[英]Account creation program is not reading file properly

I've been making a program that allows the user to create an account which saves to a txt file, and allows them to login.我一直在制作一个程序,允许用户创建一个保存到 txt 文件的帐户,并允许他们登录。 The text now saves to the file (which I was unable to make work earlier due to using w+ instead of a+ ) but I am not quite sure I understand how split() works.文本现在保存到文件中(由于使用w+而不是a+ ,我无法更早地进行工作),但我不太确定我是否理解split()的工作原理。 When I try to use the info saved to the txt file the program returns that the username cannot be found.当我尝试使用保存到 txt 文件中的信息时,程序返回找不到用户名。 If anyone could fix this code id appreciate it.如果有人可以修复此代码,我将不胜感激。 I began a few weeks ago so a lot of this is new to me.我是几周前开始的,所以很多这对我来说都是新的。


        AccountsFile = open("AccountProj.txt", "a+")
        AccountList = [line.split('\n') for line in AccountsFile.readlines()]
        
        #Creates an account 
        def createaccount():
            while True:
                
        newname = (input("Please create a username: "))
                
        if newname in AccountsFile:
            print("Username already in use.")
            continue
                
        elif newname not in AccountsFile:

            newpassword = input("Please create a password: ")

            checkpassword = input("Re-enter password: ")
                
            if checkpassword == newpassword:
                print("Account Sucessesfuly created!")
                AccountsFile.write(newname + '\n')
                AccountsFile.write(checkpassword + '\n')
                AccountsFile.close()
                break

            elif checkpassword != newpassword:
                print("Passwords do not match")
                continue
        #Logs into an account

        def loginaccount():
            while True:
                
        username_entry = input("Enter username: ")
                
        if username_entry not in AccountList:
            print("Username not found. Please enter a valid name")
            continue
        if username_entry in AccountList:
            password_entry = input("Enter password: ")
                    
            if password_entry in AccountList[AccountList.index(username_entry) + 1]:
                print("Login sucessful!")
                AccountsFile.close()
                break
            if password_entry not in AccountList[AccountList.index(username_entry) + 1]:
                print("Username and password do not match. Please try again.")
                AccountsFile.close()
                continue
while True:
        
                    
        #Asks if user wants to create or login to an account            
        loginchoice = input("Would you like to login? (Y/N) ")
        
        if loginchoice in ('Y', 'N'):
        
            if loginchoice == 'Y':
                loginaccount()

            if loginchoice == 'N':
                
                createchoice = str(input("Would you like to create an account? (Y/N) "))
                
                if createchoice in ('Y', 'N'):
                
                    if createchoice == 'Y':
                        createaccount()

                    if createchoice == 'N':
                        pass
                break
        else:
            print("Invalid Input")

Check AccountList - both split() and readlines() create a list for you, so you have a list of lists and your username_entry check can't work that way.检查 AccountList - split()readlines()都会为您创建一个列表,因此您有一个列表列表,而您的 username_entry 检查不能以这种方式工作。

def CreateAccount():
    Username = input("Username: ") #Enter Username
    Username.replace(" ", "") #Removes spaces (Optional)
    AppValid = True #Default value that changes if duplicate account found
    File = open("Accounts.txt","r") #Checking if username already exits
    for Line in File:
        Account = Line.split(",")
        if Account[0] == Application:
            print("There is already an Account with this Username")
            AppValid = False #Changing value if username exits
    File.close()
    if AppValid == True: #If username not found, carries on
        Password = input("Password: ") #Asks for Password
        CheckPassword = input("Password: ")
        if Password == CheckPassword:
            print("Password's Match!") #Password Match
        else:
            print("No match")
        File = open("Accounts.txt","a") #Writing new account to File
        File.write(Username + "," + Password + "\n")
        File.close()
def ViewAccount(Username, Password):
    File = open("Accounts.txt","r")
    Data = File.readlines()
    File.close()
    if len(Data) == 0:
        print("You have no Accounts") #Account not found since no accounts
    else:
        AccountFound = false
        for X in Data: #Looping through account data
            if X[0] == Username: #Username match
                if X[1] == Password:
                    AccountFound = true
                    print("Account Found")
        if AccountFound = false:
            print("Account not FOUND")

Theres some code i threw together (JK my hand hurts from typing that and my keyboard is screaming) but back to the point.split(" ") would split a string into a list based on spaces for that example, eg:我把一些代码放在一起(JK,我的手因打字而受伤,我的键盘在尖叫)但回到 point.split(" ") 将基于该示例的空格将字符串拆分为列表,例如:

String = "Hello There"
String = String.split(" ") #Or String.split() only owrks for spaces by default
print("Output:", String[0]) #Output: Hello
print("Output:", String[1]) #Output: There

String = "Word1, Word2"
String = String.split(", ") #Splits string by inserted value
print("Output:", String[0]) #Output: Word1
print("Output:", String[1]) #Output: Word2

String = "abcdefghijklmnop"
String = String.split("jk") #Splits string by inserted value
print("Output:", String[0]) #Output: abcdefghi
print("Output:", String[1]) #Output: lmnop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM