Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I tries to write program that will be append to the list recursively

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

and when I start the code, I got error

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

like image 777
Mikołaj Krzeszowiec Avatar asked Mar 05 '23 21:03

Mikołaj Krzeszowiec


1 Answers

when times is 0 your function prints the list but returns None. That means [list] + string(times-1 ,char) tries to concatenate None to a list and that is not allowed.

Use return instead of print and this problem will be solved.

like image 130
nosklo Avatar answered Mar 15 '23 22:03

nosklo