当我正确输入用户名时,为什么说拒绝访问?

时间:2017-10-08 18:02:56

标签: python login readfile writefile

嘿,我已经编写了这段代码,但我看不出它有什么问题,它说用户名是错的,如果我打印它,它会返回我输入的内容。

ea = input("Do you already have an account")
if ea == "Yes" or ea == "yes":
    Ausername = input("Input your username")
    Apassword = input("Input your password")

    f=open("login.txt","r")
    lines=f.readlines()
    username=lines[0]

    if (Ausername) == (username):
        print("Welcome to the quiz")
    else:
        print("Access denied")

    f.close()

else:
    name = input("Input your name")

    yeargroup = input("Input your year group")

    age = str(input("Input your age"))

    firstusername = ((name[0]+name[1]+name[2])+(age))
    print((firstusername)+(" is your username"))
    firstpassword = input("Enter what you want your password to be")
    print(firstusername)
    print(firstpassword)


    login = open("login.txt","a")
    login.write(firstusername + "\n" + name + "\n" + yeargroup + "\n" + age + "\n" + firstpassword + "\n")
    login.close()

print("---------------------------------------------------")

1 个答案:

答案 0 :(得分:2)

默认情况下,使用尾随换行符读取文件数据。在比较字符串之前,您是否尝试过调用str.strip

if Ausername == username.strip():
    ...

此外,如果您想进行不区分大小写的比较,则应使用str.lower将字符串转换为小写,以减小搜索空间的大小:

if ea.lower() == "yes":
    ...