我无法将用户输入与预填充字典值进行正确比较

2024-05-01 21:57:24 发布

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

我想在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:")

Tags: 用户密码inputyourrawdictionary字典names
2条回答

最好使用with语句自动关闭打开的文件,否则在读取文件后不要忘记关闭文件。另外,file的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]):
    ... ...

假设你能正确地读懂课文,这就行了。在

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"

相关问题 更多 >