0

I have a function which calculate the max in a list:

def max_in_list(list):
    max = 0
    for i in range(len(list)-1):
        if list[i] > list [i+1] and list [i] > max:
            max = list[i]
        elif list[i +1] > max:
            max = list [i+1]

print max       

another one to map the lenght of strings to a new list

def maps(list):
    list_integer = []
    for i in list:
        list_integer.append(len(i))

    print list_integer

and I want to calculate the longest word with this one:

def the_longest_word(list):

    new_list = maps(list)
    max_in_list(new_list)

It looks like the first function return None. My question is how can I assign the returned value to a variable so I can use it in the second function?

2
  • 1
    max_in_list doesn't return anything (None by default), it just modifies the local variable max. Commented Mar 7, 2016 at 9:25
  • 1
    You need to return something from the function in the first place... Commented Mar 7, 2016 at 9:25

1 Answer 1

4

Instead of printing you need return result at end of function :

def max_in_list(list):
    max = 0
    for i in range(len(list)-1):
        if list[i] > list [i+1] and list [i] > max:
            max = list[i]
        elif list[i+1] > max:
            max = list [i+1]
    return max

and:

def maps(list):
    list_integer = []
    for i in list:
        list_integer.append(len(i))
    return list_integer

so the last function should be like this:

def the_longest_word(list):

    new_list = maps(list)
    return max_in_list(new_list)
Sign up to request clarification or add additional context in comments.

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.