如何将登录从外部文件更改为循环?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将登录从外部文件更改为循环?相关的知识,希望对你有一定的参考价值。

我想将外部文件中的凭据读入循环,因此每次密码不正确时,它都会再次询问我。

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    file = open("accountfile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("
")
    file.close()
    if login():
        print("You are now logged in...")
    else:
        print("You aren't logged in!")

def login():
    username = input("Please enter your username")
    password = input("Please enter your password")  
    for line in open("accountfile.txt","r").readlines(): 
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            return True
    print("Incorrect credentials.")
    return False
答案

只需这样做:

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    with open("accountfile.txt","a") as file:
        file.write(username)
        file.write(" ")
        file.write(password)
        file.write("
")
    log = 0
    while log != 1:
        if login():
            log = 1
            print("You are now logged in...")
        else:
            log = 0
            print("You aren't logged in! Please try again...
")

以上是关于如何将登录从外部文件更改为循环?的主要内容,如果未能解决你的问题,请参考以下文章

如何将活动 UI 的点击传递到地图片段以将地图更改为 MAP_TYPE_HYBRID

如何在片段着色器中将分辨率从静态更改为动态

如何将登录用户更改为另一个用户?

如何在繁忙的循环中显示进度?

将徽标上的 php 链接更改为外部站点

React on Rails - 如何将登录链接更改为注销链接?