简体   繁体   English

创建一个简单的登录系统作为个人项目并在 python 中遇到一些 IO 问题

[英]Creating A Simple Login System as a personal project and having some issues with IO in python

My question is that I am creating a login system in python.我的问题是我正在用 python 创建一个登录系统。 This system is very simple.这个系统非常简单。 The user type login , his email and password.用户输入 login ,他的电子邮件和密码。 The program checks from a file whether the email or password is correct or not.该程序从文件中检查电子邮件或密码是否正确。 But I am running into some issues with that.但我遇到了一些问题。 So I first opened the file then asked the user if he wants to log in or signup.所以我首先打开文件,然后询问用户是否要登录或注册。 If he types login the program asks for email and password and check in the file whether this matches or not.如果他输入 login 程序会要求输入电子邮件和密码并检查文件是否匹配。 The program takes input correctly but at the time of checking it fails.程序正确接收输入,但在检查时失败。 It does not give an error but it does not print the desired statement.它不会给出错误,但不会打印所需的语句。 This my code这是我的代码

with open('Data_Login_System.txt' , 'r') as data:
    user_input = input("""Type Either Login or SignUp> """)
    if user_input.lower() == "login":
        Email = input("""Type your Email> """)
        Password = input("""Type Your Password> """)
        for line in data:
           if line.lower() == f"email : {Email.lower()}" or line.lower() == f"password : {Password.lower()}":
               print("Logged in")

These are the contents of the file.这些是文件的内容。

Email : shabeebasghar123@gmail.com
Password : shabeebasghar123

Whenever I enter the correct email and password it should print logged in but it does not the program just run finely and nothing at the end This is the execution of the program每当我输入正确的电子邮件和密码时,它应该打印已登录,但它不是程序运行良好,最后什么也没有 这是程序的执行

Type Either Login or SignUp
> login
Type your Email
> shabeebasghar123@gmail.com
Type Your Password
> shabeebasghar123

它的工作line.lower().strip()是只执行line.lower().strip()line.lower()

First of all you got to use a readlines() function to read lines from the .txt file with a loop.首先,您必须使用 readlines() 函数从带有循环的 .txt 文件中读取行。 You could actually read a lines without a loop on that one.你实际上可以在没有循环的情况下阅读一行。 See code below.请参阅下面的代码。

lines = data.readlines()
print(data)

The out put for that would be: ['Email : mail@gmail.com\\n', 'Password : passwd123'].输出将是:['电子邮件:mail@gmail.com\\n','密码:passwd123']。

So I would try a bit different approach.所以我会尝试一些不同的方法。 Make sure that you don't have any unwanted extra lines in txt file.确保 txt 文件中没有任何不需要的多余行。

user_input = input("Type Either Login or SignUp")
if user_input.lower() == "login":
    email = input("""Type your Email""")
    password = input("""Type Your Password""")

    with open("data.txt", 'r') as f:
        t_mail = f.readline()
        t_passwd = f.readline()
        if f"Email : {email}\n" == t_mail and f"Password : {password}" == t_passwd:
            print('You are now logged in')

This works fine for me.这对我来说很好用。

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

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