[英]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.看起来第一个函数返回 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 :您需要在函数结束时
return
结果而不是打印:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.