在类中迭代列表时,“type”对象不可iterable

2024-04-18 18:45:15 发布

您现在位置:Python中文网/ 问答频道 /正文

使用此代码时:

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()

我得到一个错误:

  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

为什么?list对我来说很合适。


Tags: inpytestforifinitisline