使用python3.4的用户名和密码程序

2024-03-29 04:54:33 发布

您现在位置:Python中文网/ 问答频道 /正文

文本文件显示如下:

苹果,碎了

其中apple是用户名,crumble是密码,用逗号分隔。在

我需要一个密码和用户名系统,以确保用户被授权。到目前为止,我已经做到了:

username = input("Please enter your name. ")
print("Your username has been created and is", username)
password = input("Now please create a password. ")

file = open("Login.txt","a")
file.write (username)
file.write (",")
file.write (password)
file.write("\n")
file.close()

它将用户用户名和密码保存在文本文件中。 那么,如何创建一个登录系统,逐行检查文本文件中的用户名和密码呢?在

例如,如果用户输入了位于第7行的密码和用户名,程序需要检查前面的所有行,直到找到用户输入的内容为止。在

I can only use python, no other programs such as Pandas or CMD.

谢谢


Tags: 用户苹果密码appleinput系统usernamepassword
1条回答
网友
1楼 · 发布于 2024-03-29 04:54:33

你可以首先从该文件创建字典,然后你可以轻松地检查用户名是否存在,然后匹配密码。在这里你需要唯一的用户名(登录系统需要它)

dict = {}
with open("Login.txt") as f:
    for line in f:
       (userName, password) = line.split(',')
       dict[userName] = password

然后检查用户名是否存在。然后像下面一样检查密码

^{pr2}$

相关问题 更多 >