alittletoohazy
alittletoohazy

Reputation: 35

Finding a string from a list made from a file in Python 3

I am trying to find a name that is in two separate lists I created and have a function check to see if it is there. I know it is checking the list and I have printed out the list to make sure it is stored correctly but it keeps giving me my error statement that the name is not found in the list. Here is the code I have for it.

def readBoyFiles():
    boyfile = 'BoyNames.txt'
    boyList = []
    with open(boyfile, 'r') as lis:
        for line in lis:
            boyList.append(line)
    return boyList

def readGirlFiles():
    girlfile = 'GirlNames.txt'
    girlList = []
    with open(girlfile, 'r') as names:
        for line in names:
            girlList.append(line)
    return girlList

def nameInput():
    name = input('Please enter the name you would like to search: ')
    list1 = readBoyFiles()
    list2 = readGirlFiles()
    findName(name, list1)
    findName(name, list2)

def findName(name, list):
    if name in list:
        print('This name is among the most popular!')
    else:
        print('This name is not among the most popular.')
nameInput()

When I throw in a print statement like print(list1), it gives me the names in this format, ['Jacob\n', ....] and when I test it it prints out my else statement regardless of what I type in for the input. I have also tried checking it with the index function and it tells me that 'Jacob' is not in list if I try that. I feel like I have to be overlooking something because I've written similar code that works properly and this is almost a mirror image of it except with different data types.

Upvotes: 3

Views: 68

Answers (2)

balderman
balderman

Reputation: 23815

A more pythonic version of your code

def load_list(file_name):
    with open(file_name, 'r') as f:
        return [name.strip() for name in f.readlines()]


def get_lists_and_user_input():
    name = raw_input('Please enter the name you would like to search: ')
    boys_list = load_list('popular_boys.txt')
    girls_list = load_list('popular_girls.txt')
    return boys_list, girls_list, name


def check_name(name, lst, _type):
    if name in lst:
        print('The name {} is a popular {} name'.format(name, _type))
    else:
        print('The name {} is NOT a popular {} name'.format(name, _type))


boys, girls, _name = get_lists_and_user_input()
check_name(_name, boys, 'boys')
check_name(_name, girls, 'girls')

Upvotes: 1

Neil
Neil

Reputation: 14313

Remember to strip your strings! It removes leading and trailing whitespace. Technically, "Jacob" isn't in the list because "Jacob\n" is.

def readBoyFiles():
    boyfile = 'BoyNames.txt'
    boyList = []
    with open(boyfile, 'r') as lis:
        for line in lis:
            boyList.append(line.strip())
    return boyList

def readGirlFiles():
    girlfile = 'GirlNames.txt'
    girlList = []
    with open(girlfile, 'r') as names:
        for line in names:
            girlList.append(line.strip())
    return girlList

Upvotes: 3

Related Questions