Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将用户输入与文本文件进行比较_Python - Fatal编程技术网

Python 将用户输入与文本文件进行比较

Python 将用户输入与文本文件进行比较,python,Python,我试图将用户输入与文本文件中的变量进行比较。每次我尝试输入学生id(00010002)时,它都会不断打印“您不是注册学生”再见。如何解决此问题?如果要将输入ID与列表进行比较,则需要添加for循环 def登录(自): 顺便说一句,正如@Cohan在评论中所说的,行中的任何字符都将授予用户访问权限。我假设这只是为了学习,而不是真正的安全方法。创建实例时,可以加载一次有效的ID。然后,当用户尝试登录时,您只需检查该ID是否存在于该集中。例如: StudentID = raw_input("p


我试图将用户输入与文本文件中的变量进行比较。每次我尝试输入学生id(00010002)时,它都会不断打印“您不是注册学生”再见。如何解决此问题?

如果要将输入ID与列表进行比较,则需要添加for循环 def登录(自):


顺便说一句,正如@Cohan在评论中所说的,行中的任何字符都将授予用户访问权限。我假设这只是为了学习,而不是真正的安全方法。

创建实例时,可以加载一次有效的ID。然后,当用户尝试登录时,您只需检查该ID是否存在于该集中。例如:

    StudentID = raw_input("please enter your student id. ")
    f = open("StudentDetails.txt", "r+")
    lines = f.readlines()
    for line in lines
        if StudentID in line:
            print("Verified Welcome")
        else:
            print("you are not a registered Student Goodbye")
            f.close()

你在使用python2吗?事实上,你正在将一个字符串
StudentID
与一个列表
-你为什么期望它工作?!您想将列表中的每个元素与字符串进行比较,例如,操作符中有
StudentID==lines
只有在用户设法键入文件的全部内容时才会为真。@jasonharper,并以某种方式欺骗
raw\u input
返回列表,如果我将我的学生ID设置为
e
,我几乎可以保证访问!也许
如果StudentID.strip()==line.strip()
@Cohan这是关于将用户输入与文本文件进行比较,而不是关于安全性。你会认为把用户保存在文本文件中是个好主意吗?那么,社区是否应该忽视这个问题呢?
    StudentID = raw_input("please enter your student id. ")
    f = open("StudentDetails.txt", "r+")
    lines = f.readlines()
    for line in lines
        if StudentID in line:
            print("Verified Welcome")
        else:
            print("you are not a registered Student Goodbye")
            f.close()
from pip._vendor.distlib.compat import raw_input

class Login:

    def __init__(self):
        with open("StudentDetails.txt", 'r') as file:
           lines = file.readlines()
        self.valid_ids = set([s.strip() for s in lines])

    def logging_in(self):

        StudentID = raw_input("please enter your student id. ")
        if StudentID.strip() in self.valid_ids:
            print("Verified Welcome")
        else:
            print("you are not a registered Student Goodbye")

login = Login()

login.logging_in()