Python read from text file IV from initials find full name
You are given a file namelist.txt that contains a bunch of names. Some of the names are a first name and a last name separated by spaces, like George Washington, while others have a middle name, like John Quincy Adams. There are no names consisting of just one word or more than three words. Write a program that asks the user to enter initials, like GW or JQA, and prints all the names that match those initials. Note that initials like JA should match both John Adams and John Quincy Adams.
namelist = [line.strip()
for line
in open('C:/Users/ZM/Desktop/Documents/New
folder/namelist.txt')]
enterInitials = input('Enter person\'s initials:')
namelist2 = [line.split() for line in namelist]
string1 = []
for i in namelist2:
if len(i) == 2 and i[0][:1].lower() + i[1][:1].lower() == enterInitials.lower():
print(' '.join(i))
elif len(i) == 3 and i[0][:1].lower() + i[1][:1].lower() + i[2][:1].lower() == enterInitials.lower():
print(' '.join(i))
elif len(i) == 3 and i[0][:1].lower() + i[2][:1].lower() == enterInitials.lower():
print(' '.join(i))
enterInitials = input('Enter person\'s initials:')
namelist2 = [line.split() for line in namelist]
string1 = []
for i in namelist2:
if len(i) == 2 and i[0][:1].lower() + i[1][:1].lower() == enterInitials.lower():
print(' '.join(i))
elif len(i) == 3 and i[0][:1].lower() + i[1][:1].lower() + i[2][:1].lower() == enterInitials.lower():
print(' '.join(i))
elif len(i) == 3 and i[0][:1].lower() + i[2][:1].lower() == enterInitials.lower():
print(' '.join(i))
SOLUTION:
Enter person's initials: ja
John Adams
John Quincy Adams
Enter person's initials: ja
John Adams
John Quincy Adams
Коментари
Постави коментар