Python Forum
Username and password
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Username and password
#1
HI guys

I need help with my code, I want to create a username and password login python program but I am not getting the right results. so far I got it to only read the first row, but sure how I can read the text in the second third row.

e.g:
john 12
mic 13
adam 13

It only reads john and 12 at this stage. and not the rest from column 0 or 1. Can you please lte me know were I am going wrong with my code please.

def user_name():
    with open("password.txt","r") as access:
        text = access.read().strip().split()
        for line in access:
            lines = line.split
            s = lines[0]
        while True:
            try:
                s = input("Enter a Username: ")
                if s == "":
                    continue
                if s in text[0]:
                    print("Username is correct ")
                    break
                raise Exception("No such string found, try again")
            except Exception as e:
                print(e)

def password():
   with open("password.txt","r") as access:
        text = access.read().strip().split()
        for line in access:
            lines = line.split
            s = lines[1]
        while True:
            try:
                s = input("Enter Password: ")
                if s == "":
                    continue
                if s in text[1]:
                    print("Password is correct")
                    break
                raise Exception("No such string found, try again")
            except Exception as e:
                print(e)



user_name()
password()
Reply
#2
Quote:
text = access.read().strip().split()
        for line in access:
            lines = line.split
            s = lines[0]

What is your intention with these lines?

To me, it looks like the for loop goes over every line in the file, and then ignores whatever happens to be in those lines. Meanwhile, text is whatever the first character of the file is.
Reply
#3
(Sep-02-2017, 10:09 PM)nilamo Wrote:
Quote:
text = access.read().strip().split()
        for line in access:
            lines = line.split
            s = lines[0]

What is your intention with these lines?

To me, it looks like the for loop goes over every line in the file, and then ignores whatever happens to be in those lines. Meanwhile, text is whatever the first character of the file is.


I got it from various tuturials online, thought it would be needed to read the text within the column
Reply
#4
You're not using lines or s, so looping over the lines in the file and assigning temporary variables does nothing.

You keep mentioning "columns".  What are you defining as a column, and what are a few of them in your example data?
Reply
#5
(Sep-02-2017, 11:01 PM)nilamo Wrote: You're not using lines or s, so looping over the lines in the file and assigning temporary variables does nothing.

You keep mentioning "columns".  What are you defining as a column, and what are a few of them in your example data?


I want it to read the username which is in column 0 and the password in column 1 but it only reads the the first text in both columns, I want it to read line 2 3 etc.
Reply
#6
Unless you split the line at word boundaries, a "column" in python is only a single character.

>>> text = '''john 12
... mic 13
... adam 13'''.split("\n")
>>> text
['john 12', 'mic 13', 'adam 13']
>>> for line in text:
...     print("Column 0: {0}".format(line[0]))
...     print("Column 1: {0}".format(line[1]))
...
Column 0: j
Column 1: o
Column 0: m
Column 1: i
Column 0: a
Column 1: d
Reply
#7
Redone the code and tried to make it more efficient, but its not accepting the password.


def login ():
    for i in range(5):
      
        user1 = (str)(input("Please enter your username: "))
        passw = (str)(input("Please enter your password: "))
        if (user1[0]) in open("password.txt").read().split("\n") and (passw[1]) in open("password.txt").read().split("\n"):
            print ("Your credentials are correct!")
            

        else:
            print ("Your credentials are incorrect!")

    print("You can no longer attempt to login!")


login()

password()
Reply
#8
Quote:
    with open("password.txt","r") as access:
        text = access.read().split("\n")
 

Ok, so text is a list of strings. Three elements. Each element of the list, is a line from the file. text[0] contains both the user name AND the password. So to check more than just the first user, loop through all of them:
s = input("Enter a username: ")
user_index = None
for index, user in enumerate(text):
    if s in user:
        # correct
        user_index = index

if user_index is None:
    print("User not found")
else:
    s = input("Enter a password: ")
    if s in text[user_index]:
        # right password, for the user they entered
Reply
#9
The last else is giving me a invald syntax not sure why?



def user_name():
   with open("password.txt","r") as access:
       text = access.read().split("\n")
       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()
Reply
#10
You have missed ':'

A few things. In Python everything is an object. The method split() is an object and you can asifn it to a 'variable' for sure: variable = string_.split. Now variable is another pointer to the split() method. In order to execute it you have to put parentesis at the end of method/function: some_string.split().

Iterating over lines in a file is simple as nothing else:
with open('file.txt', 'r') as in_file:
    for line in in_file:
        words = line.split()
input() always returns a string so you don't need to use str().
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function-decorator , which is checking an access according to USERNAME Liki 6 525 Feb-17-2024, 03:36 AM
Last Post: deanhystad
  python code tp determin a userName and print message jackAmin 4 1,800 Nov-20-2022, 12:03 AM
Last Post: rob101
  Help! Chatting App - Changing the "IP & Port" into Username Rowan99 0 1,397 Dec-20-2021, 07:02 AM
Last Post: Rowan99
  Password and Username Verification AlwaysNew 4 17,136 Nov-12-2017, 11:51 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020