-1

Hey I have wrote this code however I cannot see what is wrong with it, it is saying that the username is wrong yet if I print it, it returns exactly what i input.

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("---------------------------------------------------")
Clout
  • 3
  • 2
  • 1
    What do you mean by username is wrong? What is expected output and also please post the error traceback. – rkatkam Oct 08 '17 at 18:11

1 Answers1

2

File data is read in by default with the trailing newline. Did you try calling str.strip before comparing your strings?

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

Also, if you want to do case insensitive comparisons, you should convert your string to lowercase using str.lower to reduce the size of your search space:

if ea.lower() == "yes":
    ...
cs95
  • 379,657
  • 97
  • 704
  • 746
  • The file is opened in append mode when writing, but username is read from `readlines()[0]`. I suspect that's an issue but not gone through the code yet. – roganjosh Oct 08 '17 at 18:28
  • Thanks this worked, what does the strip bit actually mean and do? – Clout Oct 09 '17 at 14:57
  • @Clout as the name hints, it removes trailing new lines and whitespace from strings. – cs95 Oct 09 '17 at 16:15