John adams
John adams

Reputation: 5

Call function in a new function

I have a function:

def get_translations(string):
    """stuff"""
    list1 = []
    empty = []
    lines = string.splitlines()
    order = 1 if string.split(',')[0] == "english" else -1
    
    if 'english,māori' in string:
        for seperate_words in lines:
            list1.append(tuple(seperate_words.split(",")[::order]))
        return list1[1:]
    
    if 'māori,english' in string:
        for seperate_words in lines:
            list1.append(tuple(seperate_words.split(",")[::order]))
        return list1[1:]    
    
    else:
        print("Header language not recognized!")
        return empty

I am trying to call this function in a new function get_translations_from_file so I can perform what the first function is meant to do. This is because I am trying to open a file and use that file as the string for the first function. This is what I have tried to call the first function but I have had no success:

def get_translations_from_file(filename):
    """stuff"""
    file = open(filename)
    string = file.read()
    get_translations(string)

The test is:

filename = 'places.txt'
for terms in get_translations_from_file(filename):
    print(terms)

The contents of the file is:

english,māori
New Zealand,Aotearoa
North Island,Te Ika-a-Māui
South Island,Te Waipounamu
Wellington,Te Whanganui a Tara
Christchurch,Ōtautahi
Hamilton,Kirikiriroa
Auckland,Tāmaki Makaurau

Upvotes: 0

Views: 74

Answers (1)

Mureinik
Mureinik

Reputation: 312146

You are calling get_translations, but ignoring the return value. Since get_translations_from_file has no explicit return statement, it implicitly returns None. To make a long story short, you need to return the value from get_translations:

def get_translations_from_file(filename):
    """stuff"""
    file = open(filename)
    string = file.read()
    return get_translations(string) # Here!

Upvotes: 1

Related Questions