2

hi i'm complete confused on how to do what i want, i'm basically checking a file that it has two words in for this instance the username and password for a user.

text = open("Accounts.dat", 'w')
text.writelines("the username")
text.writelines("\n")
text.writelines("the password")
text.close()

username = input("Enter username: ")
password = input("Enter password: ")
data = open("Accounts.dat").read()
if username and password in data:
  print("works")
else:
  print("doesn't work")
data.close()

this code does work in some ways, like if i just enter the correct username and nothing for the password it prints("works") but if i enter nothing into username and password it prints("doesn't work"), then if i just type the correct password and nothing for the username it doesn't work.

i need it so it only prints ("works") when both the username and password are correct.

1
  • This isn't a good scheme. For example, you could just enter the username again for the password. Commented May 15, 2013 at 21:43

1 Answer 1

3

if username and password in data: should be

if username in data and password in data:

When you do if username and password in data:, the way python would interpret it is:

if (username) and (password in data):

which is, check if username is True and check if password is in data

Sign up to request clarification or add additional context in comments.

4 Comments

Such an easy mistake to make, when the language resembles English so much.
It's not just that username is not None. An empty string would also evaluate to False. It's more that username.__bool__ is True
@karthikr that does work but now, when there is nothing it prints ("works"), when there is just the correct password it prints ("works") and when there is just the correct username it prints ("works"), if the username is correct and password is wrong it prints("doesn't work") and the same for correct password and incorrect username
You can then do if username and password and username in data and password in data

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.