0

我正在尝试在 python 中制作任意用户名和密码程序我有一个字典,其中填充了一系列用户名和值我无法将用户输入与字典用户名和值进行比较。它们似乎不相等。

textFile = open('names.txt','r')
dictionary = {}
for lines in textFile:
    splatLine=lines.split('\t')
    dictionary[splatLine[3]]= splatLine[4]
print dictionary

userName= raw_input("what is your UserName:")
password= raw_input("what is your Password:")
4

2 回答 2

0

最好使用with语句自动关闭打开的文件,否则不要忘记在读取文件后关闭文件。此外,文件的readline方法将返回带有尾随换行符的行,您需要在与用户输入进行比较之前将其剥离。

with open('names.txt','r') as f:
    pwd_dict=dict([line.strip().split('\t')[3:5] for line in f])    
userName= raw_input("what is your UserName:")
password= raw_input("what is your UserName:")
if not (username in pwd_dict and password == pwd_dict[username]):
    ... ...
于 2012-09-03T01:48:04.543 回答
0

假设您正确阅读了文本,这将起作用。

dictionary = {"foo":"bar","Johnny":"Appleseed"}
uname = "foo"
pw = "nobar"
     for i in dictionary:
          if uname == i:
              if dictionary[i] == pw:
                   print "You're in"
              else:
                   print "all your base are belong to us"
于 2012-09-03T01:12:20.697 回答