0

This is a text file accountinfo.txt:

Luke TonyHawk123! [email protected]  
Cindy JasonVoorhees123! [email protected]

I want to ask the user for their email to print the values (i.e. their username and password) on the left. For example, if the user inputs [email protected], it should return Luke and TonyHawk123.
I've tried using strip and split, but my logic is wrong.

Work I've done so far:

account_file = open("accountinfo.txt")
email_string = account_file.read().strip().split()
while True:
    email_account = input("Enter the email linked to your account: \n")
    if email_account == "":
        continue
    if email_account in email_string:
        # ???
    else:
        print("This email doesn't exist in our records.")
        main()
1
  • Checking email_account in email_string doesn't work, because ("Luke", "TonyHawk123!", "[email protected]") is what's in email_string. You would need to extract the email address and probably put it into a dictionary for retrieval. Commented Jan 25, 2020 at 20:55

2 Answers 2

1

You can apply csv.reader here:

import csv

with open("accountinfo.txt") as f:
    reader = csv.reader(f, delimiter=" ")
    email = input("Enter the email linked to your account: \n")
    for row in reader:
        if row and row[2] == email:
            print(" ".join(row[:2]))
            break
    else:
        print("This email doesn't exist in our records.")
        main()

You can also split each line manually:

with open("accountinfo.txt") as f:
    email = input("Enter the email linked to your account: \n")
    for line in f:
        if email and email in line:
            print(line.rsplit(" ", 1)[0])
            break
    else:
        print("This email doesn't exist in our records.")
        main()
Sign up to request clarification or add additional context in comments.

Comments

0

I don´t know how the information of each e-mail is arranged but if they´re in the same file. I did a similar program, but the information of each username was separated with a sting like this: "----------------". So I iterated through lines until it found the e-mail. Once it found it, I would print every line until it found another "----------------". So all the user´s information was between those lines. You can do something similar depending on how your information is arranged. You can iterate through the lines of the file with:

validation = False
for line in file:
   if email in line:
      validation = True
      while line != "---------------":
         print(line, end="")

if validation == False:
  print("This email doesn't exist in our records.")

1 Comment

There's for .. else statement in python which designed specially for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.