bk2014111825 2021-11-04 17:00 采纳率: 80%
浏览 1139
已结题

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

```

  • 写回答

3条回答 默认 最新

  • 於黾 2021-11-04 17:02
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 已采纳回答 11月4日
  • 创建了问题 11月4日