gpt4 book ai didi

python - 类型错误 : can only concatenate list (not "NoneType") to list

转载 作者:行者123 更新时间:2023-12-02 20:59:33 26 4
gpt4 key购买 nike

我尝试编写将递归附加到列表的程序

def string(times,char):
list=[]
list.append(char)
if times==0:
print(list)
else:
return [list] + string(times-1 ,char)
string(3,input('text'))

当我启动代码时,出现错误

TypeError: can only concatenate list (not "NoneType") to list

最佳答案

times0时,您的函数将打印列表但返回None。这意味着 [list] + string(times-1 ,char) 尝试将 None 连接到列表,但这是不允许的。

使用return代替print,这个问题就可以解决。

关于python - 类型错误 : can only concatenate list (not "NoneType") to list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51199082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com