【问题标题】:Searching a string stored in a text file搜索存储在文本文件中的字符串
【发布时间】:2019-10-27 09:42:44
【问题描述】:

我正在编写一个代码来实现一个用户登录系统,用户将在其中输入他的详细信息,例如姓名和密码,这些信息将存储在一个文本文件中。当他尝试登录时,它会询问用户名和密码,并与之前创建的文本文件中存储的用户名和密码相匹配。

可能是无法正确搜索文本文件

user=str(input('Do u have registration(Y/N): \n '))
if user =='Y':
    print('Please login now:')
    boom=str(input("Enter your name: "))
    pass_inp=str(input('Enter your password: \n'))


    with open('user_database.txt','r') as f:
        if pass_inp in f.readlines() and boom in f.readlines():
            print('Welcome User')
        else:
            print('Invalid Login ID')
elif user == 'N':
    print('Please register now,fill the following details:')
    name=str(input("Register your name: \n"))
    password =str(input('Create your password:\n'))
    with open("user_database.txt",'a') as f:
        f.write(name+'\n')

        f.write(password+'\n')

        print('Account successfully created')`````````

【问题讨论】:

    标签: python search


    【解决方案1】:

    抛开将密码和用户作为纯文本保存在文件中的原因,并允许用户名和密码相同, 请检查f.write(name+'\n')中的“\n”是否导致问题。(肯定是)

    【讨论】:

      【解决方案2】:

      您正在使用

      读取文本文件两次
      if pass_inp in f.readlines() and boom in f.readlines(): 
      

      将内容设置为变量,然后检查变量的名称和密码。

      contents =  f.readlines()
      
      if pass_inp in contents and boom in contents:
      

      Previous solution to this problem。此解决方案的优势在于,只需进行少量编码更改,即可在检查当前方法无法区分的用户名和密码时区分用户名和密码。

      【讨论】:

        【解决方案3】:

        希望 cmets 帮助你理解逻辑:

        # input return a string
        user = input('Do u have registration(Y/N): \n ')
        if user == 'Y':
            print('Please login now:')
            boom = input("Enter your name: ")
            pass_inp = input('Enter your password: \n')
        
            with open('user_database.txt','r') as f:
                lines = f.readlines()
                flag_founded = False
                for index, line in enumerate(lines):
                    # user name found check if the line after it contains it's password
                    if boom == line.replace('\n', ''):  
                        # password of this user should be in the next line
                        password = lines[index+1].replace('\n', '')
                        if pass_inp == password:
                            flag_founded = True
                        break  # break the current loop no need to keep checking because user name should be unique
                if flag_founded:
                    print('You are logged: ', boom)
                else:
                    print('User name or password wrong')
        
        elif user == 'N':
            print('Please register now,fill the following details:')
            name = input("Register your name: \n")
            password = input('Create your password:\n')
            with open("user_database.txt", 'a') as f:
                f.write(name+'\n')
                f.write(password+'\n')
                print('Account successfully created')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 2015-11-02
          相关资源
          最近更新 更多