简体   繁体   中英

Assigning return value of a function to a variable in Python

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM