如何使用文本文件创建登录

时间:2017-12-12 21:17:25

标签: python logging login append

所以我一直在尝试创建一个测验,你必须拥有一个帐户,这意味着你可以注册和登录。我设法编写了注册部分(我非常自豪)并将登录详细信息保存到单独的文本文件中,当它保存登录详细信息时,它看起来像这样:username:password

现在我正在努力使用登录部分,我认为您必须阅读文本文件然后拆分用户名和密码,然后我将如何将输入的用户名和密码与保存的用户名和密码进行比较。 这是我到目前为止为登录部分所做的事情,但它不起作用:

def login():
filename = 'Accounts.txt'
openfile = open('Accounts.txt', "r")
Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
    for line in file:
        user2, passw = line.split(':')
        login2 = input("Enter username: ")
        passw2 = input("Enter passwordd: ")
        if login2 == user2 and passw2 == passw:
            print("Logged in")
        else:
            print("User or password is incorrect!")
openfile.close();

现在这就是整个代码的样子(如果需要):

import time

print("Welcome to my quiz")

#Creating username
def create():
    print ("We will need some information from you!")
    time.sleep(1)
    Veri = input("Would you like to continue (yes or no): ")
    def createAccount():
        while True:
            name = input("Enter your first name: ")
            if not name.isalpha():
                print("Only letters are allowed!")
            else:
                break
        while True:
            surname = input("Enter your surname: ")
            if not surname.isalpha():
                print("Only letters are allowed!")
            else:
                break
        while True:
            try:
                age = int(input("Enter your age: "))
            except ValueError:
                print("Only numbers are allowed!")
                continue
            else:
                break
        if len(name) >= 3:
            username = name[0:3]+str(age)
        elif len(surname) >= 3:
            username = surname[0:3]+str(age)
        else:
            username = input("Create a username: ")
        print ("Your username is:",username)
        while True:
            password = input("Create a password: ")
            password2 = input("Confirm your password: ")
            if password != password2:
                print("Password does not match!")
            else:
                break 
        account = '%s:%s\n'%(username,password)
        with open ('Accounts.txt','a') as file:
            file.write(account)
            print ('Account saved')   
    if Veri == 'no':
        menu()
    elif Veri == 'yes':
        createAccount()
    else:
        print ("Sorry, that was an invalid command!")
    time.sleep(1)
    login()

#Loging in
def login():
    filename = 'Accounts.txt'
    openfile = open('Accounts.txt', "r")
    Userdata = openfile.readlines()
    with open('Accounts.txt', 'r') as file:
        for line in file:
            user2, passw = line.split(':')
            login2 = input("Enter username: ")
            passw2 = input("Enter passwordd: ")
            if login2 == user2 and passw2 == passw:
                print("Logged in")
            else:
                print("User or password is incorrect!")
    openfile.close();

time.sleep(1)

#Choosing to log in or register
def menu():
    LogOrCre = input("Select '1' to login or '2' to register: ")
    if LogOrCre == '1':
        login()
    elif LogOrCre == '2':
        create()
    else:
        print ("Sorry, that was an invalid command!")
        menu()

menu()

如果您对我如何制作登录部分有任何想法,那将会有所帮助。

3 个答案:

答案 0 :(得分:1)

您要求帐户文件中每一行的用户名和密码。在for循环之外获取输入的登录信息。

答案 1 :(得分:1)

欢迎编程! 这可能非常令人生畏,但继续学习和练习。你的方式正确!

我在代码中看到了一些可以改进的要点。我在下面发表评论:

1)您不需要同时使用open()(和close())和with来访问文件的内容。在这种情况下,只需使用with即可。它会让你的代码更简单。 (A good answer about with

2)您要求用户登录& passwd在循环内(又称多次)。这对您的用户来说非常烦人。将input的调用移至for循环之前。

3)当登录成功时,您还需要break循环。

因此,稍微改进的代码版本将是:

filename = 'Accounts.txt'
with open(filename, 'r') as file:
    login2 = input("Enter username: ")
    passw2 = input("Enter password: ")

    for line in file:
        user2, passw = line.split(':')
        if login2 == user2 and passw2 == passw:
            print("Logged in")
            break
        else:
            print("User or password is incorrect!")

答案 2 :(得分:0)

不确定您的代码中有什么不起作用,但我更新了以下代码,并且能够打印登录。

def login():
#filename = 'Accounts.txt'
#openfile = open('Accounts.txt', "r")
#Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
    login2 = input("Enter username: ")
    passw2 = input("Enter passwordd: ")
    for line in file:
        user2, passw = line.split(':')
        if login2 == user2 and passw2 == passw:
            print("Logged in")
            break
        else:
            continue

login()
相关问题