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
  Working with CSV data and iterating through a file skoobi 1 99 Aug-13-2019, 03:28 PM
Last Post: Gribouillis
  Appending data into a file in tab delimited format metro17 1 98 Aug-06-2019, 07:34 AM
Last Post: fishhook
  export file and display cmd prompt same time jacklee26 1 91 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  How to save latest time stamp in a file? redwood 12 717 Jul-11-2019, 11:03 AM
Last Post: redwood
  Load JSON file data into mongodb using pymongo klllmmm 1 230 Jun-28-2019, 12:47 AM
Last Post: klllmmm
  Parse data from xml file klllmmm 9 671 Jun-25-2019, 05:14 PM
Last Post: heiner55
  How to conver txt file data to python? yunglin 1 188 Jun-13-2019, 07:50 PM
Last Post: micseydel
  Parse Binary Data File and convert Epoch Time drdevereaux 1 230 May-16-2019, 01:56 AM
Last Post: Larz60+
  Why does time.sleep(wait) add time in python3 ? gerardg 4 514 Apr-28-2019, 06:26 PM
Last Post: gerardg
  Reading data into a Combobox from a .CSV file KevinBrown 7 709 Apr-14-2019, 11:30 AM
Last Post: KevinBrown

Forum Jump:


Users browsing this thread: 1 Guest(s)