Python Forum

Full Version: Username and password
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

Still the same problem with the else :(

def user_name():
   with open("password.txt","r") as access:
       for line in access:
          words = line.splite()
       s = input("Enter a username: ")
       user_index = None
       for index, user in enumerate(text):
           if s in user:
               user_index = index
               if user_index is None:
                   print("User not found")

                   else: #invalid  syntax
                       s = input("Enter a password: ")
                       if s in text[user_index]:
                           #user_name()
In line 4, you misspelled word. Line 13's indentation is incorrect, it should line up with it's respective "if"
I have adjusted the else, it now keeps highlighting the last if and stating it is an unexpected eof error. Not sure why.


def user_name():
   with open("password.txt","r") as access:
       for line in access:
          word = line.splite()
       s = input("Enter a username: ")
       user_index = None
       for index, user in enumerate(text):
           if s in user:
               user_index = index
               if user_index is None:
                   print("User not found")

               else:
                  s = input("Enter a password: ")
                  if s in text[user_index]:
Got it working however its not doing what I require at this stage. for instance in my textfile
I have
John go
Adam no

It doesnt accept Adam a username or the password


def user_name():
    # open the file and read data
    text = []
    with open("password.txt","r") as access:
        for line in access:

            text.append(line.split())  # store data in a list
 
    # continue with rest of the code
    name = input("Enter a username: ")
    for index, user in enumerate(text):
        user_index = None  # moved to inside the loop
        if name in user:
            user_index = index
        if user_index is None:
            print("User not found")
  
        else:
            password = input("Enter a password: ")
            if password in text[user_index]:
                print("password correct")
user_name()
Pages: 1 2