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.

1
  • 1
    How are the boys names in BoyNames.txt stored? If you have a name line by line then at the end of each name will be a newline character \n added to the name therefore when you enumerate over the file pointer you are reading lis you are reading everything line by line including the newline character. You can just remove the newline after reading it in like boyList.append(line.replace("\n", "")) Commented Feb 26, 2019 at 16:49

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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')

Comments

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.