-1

I don't see why it only displays wrong it only displays the last line it worked when it used the username and password in the script?

def login():

 for username in range(3):
   username = input("Enter your username:")
   password = input("Enter your password:")


   d="example1"
   k="example2"
   s="school"

   f=open("bob.txt")
   lines=f.readlines()

   if username == lines[0] and password == lines[1]:
        print("welcome to soar valley",d)
        break
    
   if username == lines[2] and password ==lines[3]:
        print("welcome to soar valley",s)
        break

   if username == lines[4] and password ==lines[5]:
        print("welcome to soar valley",k)
        break

   if username or password != lines[0] or lines[1] or lines[2]or lines[3]or
   lines[4] or lines[5]:
        print("wrong try again")


login()
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I think this does not do what you want it to do: `if username or password != lines[0] or lines[1] or lines[2]or lines[3]or lines[4] or lines[5]`. You can't just translate word for word from English. – Arndt Jonasson Jun 10 '18 at 17:31

1 Answers1

0

You misused the operator or.

if username or password != lines[0] or lines[1] or lines[2]or lines[3]or lines[4] or lines[5]:

This line is not executed as what you was expected.

In the Python 3.7 documentation of or,

the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.

So, a not-empty string is always interpreted as True, which means your target line can be simplified under the supposed condition (username and password are not empty, and the file "bob.txt" contains at least 6 non-empty lines at the beginning):

if True or True != True or True or True or True or True or True:

Which become always true. And this is exactly what you don't expected.

For your situation, you may try the keyword not in:

if username not in lines[0:5] and password not in lines[0:5]:

Better to use a dict to make a username-password pair.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • What have you typed and what output you seen lets you know it 'still don't work'? – Geno Chen Jun 11 '18 at 17:35
  • usually if the password is correct it should output "welcome to soar valley" but what it actually outputs is "wrong try again" – dev luhar Jun 12 '18 at 16:15