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!")
Recommended Tutorials: