1

I'm trying to create list of factors using index.

 n =int(input())
 a=list()
 b=list()

 for i in range(1,n+1):
      for j in range(1,i+1):
          if i%j==0 :
              b= b.append(j)
      a=a.append(b)
 print(a)

getting error message as follow:

 10
 Traceback (most recent call last):
 File "E:/Projects/Chatbot/assign1.py", line 23, in <module>
 a=a.append(b)
 AttributeError: 'NoneType' object has no attribute 'append'
Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 1
    The problem is that you are redefining `a` on the line where you do `a=a.append(b)`. The call to `append()` returns None and you are assigning that to `a` so `a` no longer refers to a list. – Mike Driscoll May 17 '17 at 15:03
  • @abccd the answers there explain the error in this post. This is a (the?) canonical duplicate for this kind of questions. – vaultah May 17 '17 at 15:05
  • @abccd there you go: http://stackoverflow.com/q/41452819 That question is essentially the same as this one, yet it was still closed as a duplicate of the canonical question. – vaultah May 17 '17 at 15:10
  • I like that question better, haha:) – Taku May 17 '17 at 15:12
  • Use list comprehension: n = int(raw_input('choose a number')) for i in range(1,n+1): a = [ j for j in range(1,i+1) if i%j==0 ] print 'a = ', a # Example: n = 3 returns a = [1, 3] – Gustav Rasmussen May 17 '17 at 15:16

0 Answers0