Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Data is in and not in the file at the same time
#1
Hello everyone!

At the moment I'm working on a simple Regiter/Login project which uses text file to store all the data.

username=input("Please enter your username: ")
    with open("Data.txt", "r") as DataFile:
        for i,line in enumerate(DataFile):
            if username in line:
                print("Found the user in line:",i)
                data=line.split()
                password=data[1]
                print(password)
        if username not in DataFile:
            print("Username ",username," not found!")
I get no error messages, but the problem is that both if statements are correct- at the same time username is and not in the file. Any help would be appreciated.


The output:

Please enter your username: Jeff
Found the user in line: 1
123
Username Jeff not found!


DataFile:

Jeff 123
Sandra 753
Mike 482
Quote
#2
You are checking if username is in the entire line...which it is. You should split the data first and compare it only to the first element (since the username is always first)
    for i,line in enumerate(DataFile):
        data=line.split()
        if username in data[0]:
            password = data=[1]
And for the second if clause you could use a boolean to compare between all loops.
username=input("Please enter your username: ")
found = False
with open("Data.txt", "r") as DataFile:
    for i,line in enumerate(DataFile):
        data=line.split()
        if username in data[0]:
            password = data=[1]
            found = True
            
if not found:
    print("Username {} not found!".format(username))
Or you could use a function to return found boolean
username=input("Please enter your username: ")
with open("Data.txt", "r") as DataFile:
    lines = DataFile.readlines()
    
def user_found(username, lines):
    for line in lines:
        data=line.split()
        if username in data[0]:
            return True

if not user_found(username, lines):
    print("Username ",username," not found!")
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  from Json Time Serie file to python dictionnary Reims 1 76 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  export file and display cmd prompt same time jacklee26 1 132 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  How to save latest time stamp in a file? redwood 12 778 Jul-11-2019, 11:03 AM
Last Post: redwood
  Parse Binary Data File and convert Epoch Time drdevereaux 1 262 May-16-2019, 01:56 AM
Last Post: Larz60+
  Sorting csv or txt file on date and time AshBax 4 544 Mar-27-2019, 02:02 PM
Last Post: AshBax
  Collect real time data jeuvrey 0 381 Dec-10-2018, 11:05 AM
Last Post: jeuvrey
  Extract data from rasters to plot time-series zhl 0 479 Sep-20-2018, 09:45 PM
Last Post: zhl
  Display 20 records at a time,data structure or loop pythonds 1 564 Mar-29-2018, 11:09 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)