-1

Im doing a homework assignment and will need to locate a specific word in python. Any help would be much appreciated. I am quite new to coding. I would like help on how to answer this question.

I have seen multiple tutorial's but none have helped me so far.

y=()
answer=str(input("Do you want to create an account, y or n?"))
if answer=="y":
  file = open("database.txt", "r")
  username = input("Enter a username :")
  password = input("Now enter a password :")
  file = open("database.txt","a")
  file.write (username)
  file.write (",")
  file.write (password)
  file.write("\n")
  file.close()

else:
  username1=input("Enter your username:") 
  password1=input("Now enter your password:") 
  for line in open("database.txt","r").readlines():
    login_info = line.split()
  if username1 == login_info and password1 == login_info:
    print("Incorrect")
  else:
    print("Correct")

I expected the output to say correct when all the criteria is met but instead it outputs correct when i enter anything.

3
  • I am pretty sure the indentation is mixed up here, as you have two same level 'else'-statements. Could you fix this please (as it matters immensely in python)?
    – Eric
    Jun 2, 2019 at 10:10
  • Sorry, I have fixed it now.
    – user11567668
    Jun 2, 2019 at 10:13
  • 1
    Unrelated: you're opening the file but forgetting to close it in the first else.
    – TrebledJ
    Jun 2, 2019 at 10:19

1 Answer 1

2

The indentation in the second part of your code is messed up, because the if- and else-statement should be inside of the for loop. Also you split the loaded lines into a list (login_info) but incorrectly check that against the username and password variables. And you use the default split() function which uses a whitespace as a separator, but you use a comma. I also put the else statement out of the for loop, because otherwise it will print every time the line is not the one where the user is stored. Try this for the second part:

else:
  username1=input("Enter your username:") 
  password1=input("Now enter your password:") 
  for line in open("database.txt","r").readlines():
    login_info = line.split(",")
    if username1 == login_info[0] and password1 == login_info[1].replace("\n", ""):
       print("Correct")
       break #Exit the for loop if the user is found
  else: #Only executed when break is not called
    print("incorrect")
11
  • Afterwards, Whatever i enter it prints correct twice.
    – user11567668
    Jun 2, 2019 at 10:24
  • Should it print correct when the user is already in the database? Or incorrect?
    – Eric
    Jun 2, 2019 at 10:25
  • It should print correct when the password and username are already in the database and when they are not it should print incorrect.
    – user11567668
    Jun 2, 2019 at 10:26
  • Okay then the print statements where mixed up in the first place, I will fix it :)
    – Eric
    Jun 2, 2019 at 10:27
  • To be able to print incorrect if it is not there, I would advise putting it inside a function and utilising the return keyword.
    – Eric
    Jun 2, 2019 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.