报错TypeError: 'type' object is not iterable

报错TypeError: 'type' object is not iterable

问题描述:

只用函数调用没有报错,加了一个类class serious_time:后,报错:TypeError: 'type' object is not iterable

怎么修改,确保类名,方法的方式调用正常


```python
class serious_time:
    serious_time = []
    for i in range(2, ws.max_row + 1):
        if ws['D' + str(i)].value == 'Android'  and ws['I' + str(i)].value == 'a':
            serious_time.append(ws.cell(row=i, column=11).value * 24)
    def serious_time_rate(self):
        t = 0
        for j in serious_time:
            if j <= 24:
                t = t + 1
        if len(serious_time) == 0:
            return '/'
        else:
            serious_time_rate = t / len(serious_time)
            return "%.2f%%" % (serious_time_rate * 100)

if __name__ == '__main__':
    print(serious_time.serious_time_rate())

```

你加个类,代码得写到方法里,不能直接在类里写for循环
你可以把for循环写到__init__构造函数里

ws.max_row---这ws变量从哪来的呢?

正常的代码


```python
    def serious_time_rate():
        t = 0
        for j in serious_time:
            if j <= 24:
                t = t + 1
        if len(serious_time) == 0:
            return '/'
        else:
            serious_time_rate = t / len(serious_time)
            return "%.2f%%" % (serious_time_rate * 100)
if __name__ == '__main__':
    print(serious_time_rate())

```