2017-10-21 74 views
-1

我创建了这个脚本,可以用作登录,然后创建一个外部文本文件,其中包含数据1234,这是试图比较文件中的数据,但输出这两个值是不同的,尽管它们是相同的。在此先感谢任何帮助,您可以给我,我用的代码如下:关于外部python文件的条件语句问题

getUsrName = input("Enter username: ") 
file = open("documents/pytho/login/cdat.txt", "r") 
lines = file.readlines() 
recievedUsrName = lines[1] 
file.close() 
print(getUsrName) 
print(recievedUsrName) 
if recievedUsrName == getUsrName: 
    print("hello") 
elif getUsrName != recievedUsrName: 
    print("bye") 
else: 
+0

你想用'lines [1]'来读取文件中的第二行? 或第一个用'lines [0]'? – 0TTT0

+1

@ 0TTT0我实际上有第一行,我想读第二行,我只是简化它的描述。 –

+0

当您执行它时,您必须添加文本文件的确切内容和脚本的输出。 – thanasisp

回答

0

尝试这样的:

if recievedUsrName.strip() == getUsrName: 
    ... 

它必须是结尾的新行。

+0

我刚刚尝试过,它只给出'else'的值(我忘了在上面的代码中包含这个值) –

+0

你能打印'lines [1]'吗? – zipa