I wrote a program which gets a number n ( > 0) and prints all the numbers with n digits.

def Q(n, index = 0, list = []):

        if (index == n):
                print list

        startFrom = 0
        if (index == 0 and n > 1):
                startFrom = 1

        for i in range(startFrom, 10):
                Q(n, index + 1, list)
                list = list.__delitem__(index)


When I run the code, I get:
[1, 0]

Traceback (most recent call last):
File "py_Q.py", line 16, in ?
File "py_Q.py", line 13, in Q
Q(n, index + 1, list)
File "py_Q.py", line 12, in Q
AttributeError: 'NoneType' object has no attribute 'append'

After the program prints the first number (as list), the list becomes NonType.

Could someone explain me what's happening here???

Thanks in advance!

OK. I solved it.
Instead of using __delitem__ I just used the pop function.

I still need a good explanation, because I want to understand the principals that caused my program to fail.

NoneType means that delitem deletes in place so
works for your code, and
list = list.__delitem__(index)
binds "list" to the return value=None. Also, choose a name other than "list" as that is already used by Python to convert to a list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.