python函数运行后none_Python函数返回None,不清楚为什么

我对python很陌生,遇到了我无法解释的问题。我尝试在此处搜索论坛答案,但发现与我的情况不符。感觉好像我缺少一些基本的东西,但是我没有看到(显然…)

这段代码按照我期望的方式运行:

import string

mults = [1,2,3,4,6,7,9,10,12,15,16,19,21,22,24]

def factor_exp(lst):

if lst[-1] == 1:

lst.pop()

return lst+[1]

if lst[-1] == 2:

lst.pop()

return lst+[1,1]

else:

return "Should never get here"

print factor_exp([1])

print factor_exp([2])

print factor_exp([1,2])

返回:

>>>

[1]

[1, 1]

[1, 1, 1]

这就是我想要的。

我认为在函数内部的列表上使用追加和扩展也可以工作。在代码底部附近添加了一个“追加”。

import string

mults = [1,2,3,4,6,7,9,10,12,15,16,19,21,22,24]

def factor_exp(lst):

if lst[-1] == 1:

lst.pop()

return lst+[1]

if lst[-1] == 2:

lst.pop()

return lst.append([1,1])

else:

return "Should never get here"

print factor_exp([1])

print factor_exp([2])

print factor_exp([1,2])

但这返回:

>>>

[1]

None

None

为什么出现“无”?在此先感谢您的帮助或见解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值