我有一个包含以下数据的文本文件: 温兆伦, 罗伯特, 马修,
现在,我希望用户输入一个字符串,然后将该字符串与文本文件进行比较..
fo = open("d:\\ar.txt", "r")
file_contents = fo.read()
print('Enter your password.')
typedPassword = input()
for i in file_contents:
if typedPassword == file_contents:
print('Access granted')
else:
print('Access denied')
但是,这似乎不起作用。有什么想法吗?
答案 0 :(得分:2)
试试这个: -
fo = open("poopy.txt", "r")
file_contents = fo.read()
print('Enter your password.')
typedPassword = input()
Flag = 0
for i in file_contents.split('\n'):
if typedPassword == i:
Flag = 1
if Flag == 1:
print('Access Granted')
else:
print('Access Denied')
答案 1 :(得分:0)
您正在检查输入的密码是否与整个文件匹配... 这会检查每一行
username = input("Input your username")
password = input("Input your password")
correct = None
with open("d:\\ar.txt", "r") as file:
for line in file:
line = line.split(",")
if username == line[0] and password == line[1]:
correct = True
if correct:
print("") # placeholder
#(logged in code)
为此工作你的文件应该是fornat
用户名,密码,
用户名,密码,
用户名,密码,