Posts: 10
Threads: 2
Joined: Jun 2017
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()
Posts: 3,458
Threads: 101
Joined: Sep 2016
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.
Posts: 10
Threads: 2
Joined: Jun 2017
(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
Posts: 3,458
Threads: 101
Joined: Sep 2016
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?
Posts: 10
Threads: 2
Joined: Jun 2017
(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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
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
Posts: 10
Threads: 2
Joined: Jun 2017
Sep-03-2017, 12:16 AM
(This post was last modified: Sep-03-2017, 12:16 AM by Steve2017.)
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()
Posts: 3,458
Threads: 101
Joined: Sep 2016
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
Posts: 10
Threads: 2
Joined: Jun 2017
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()
Posts: 2,955
Threads: 48
Joined: Sep 2016
Sep-03-2017, 02:12 AM
(This post was last modified: Sep-03-2017, 02:23 AM by wavic.)
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().
|