Python - 从计数返回无值

时间:2015-04-02 09:02:46

标签: python count return

我试图使用以下代码计算句子中的最大空格数:

def spaces(sentences):
  textList = sentences.split('. ')
  whiteList = [whitespaces.count(' ') for whitespaces in textList]
  x = max(whiteList)
  print(x)

但是,当它返回空白的数量时,这也会在第二行返回None。这是怎么回事?

2 个答案:

答案 0 :(得分:0)

您打印结果,但不归还。您需要在代码末尾添加return x

您可以查看this page,它解释了Python中printreturn之间的区别。

答案 1 :(得分:0)

您必须从函数返回一个值,然后打印它的结果

def spaces(sentences):
  textList = sentences.split('. ')
  whiteList = [whitespaces.count(' ') for whitespaces in textList]

  return max(whiteList)

print(spaces(sentences))
相关问题