Qing li
Qing li

Reputation: 53

'type' object is not iterable when iterating list in class

When using this code:

import random

class result:
    list = ['a', 'b', 'c']

    def choicename(self):
        for i in list:
            if i == list[0]:
                result = random.randint(90, 100)
            else:
                result = random.randint(0, 10)

        print 'The %s result is %d' % (i, result)

if __name__ == '__main__':
    t = result()
    t.choicename()

I get an error:

  File "D:\Program\test\test\__init__.py", line 22, in <module>
    t.choicename()
  File "D:\Program\test\test\__init__.py", line 13, in choicename
    for i in list:
TypeError: 'type' object is not iterable

Why is that? list looks iterable to me.

Upvotes: 1

Views: 12219

Answers (1)

Ry-
Ry-

Reputation: 224904

list in choicename refers to the list type. If you want result.list, that’s what you’ll need to use.

Upvotes: 2

Related Questions