You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Type error: 'function' does not support indexing错误?

解决TypeError: 'function' does not support indexing错误及代码优化

1. 触发报错的核心问题(第12行)

  • len是Python内置函数,获取列表长度必须用括号调用len(oof),你写成len[oof]是把函数当成列表来索引,这就导致了'function' does not support indexing错误。
  • 另外random.randint()要求传入两个参数(最小值和最大值,闭区间),你当前只传了一个参数,运行时还会触发参数不足的错误。如果是想从oof里随机选一个索引,正确写法是:
    ooo = random.randint(0, len(oof)-1)
    
    要是直接想随机选oof里的元素,用random.choice()更简单:
    ooo = random.choice(oof)
    

2. 代码里的其他潜在问题

  • taskslist函数的循环逻辑有问题:你用for i in tasks遍历任务列表,但每次都输出tasks[0],会重复打印第一个任务probe。而且每次循环y坐标都设为179,所有文字会重叠在一起。修正后的函数应该是:
    def taskslist():
        turtle.goto(-200,190)
        turtle.color('green')
        turtle.write('TASKS TO COMPLETE')
        y = 179  # 初始y坐标放在循环外
        turtle.hideturtle()  # 只需要调用一次,不用循环重复执行
        for task in tasks:
            turtle.goto(-200, y)
            turtle.write(task)
            y -= 20  # 每次循环y坐标下移,避免文字重叠
    

修正后的完整代码

import turtle
import time
import random

ct = 0  
tasks = [
    'probe',
    'O2',
    'spacewalk (E.V.A.)',
    'exersize',
]
oof = ['0', '1', '2', '3']
# 修正第12行的错误
ooo = random.randint(0, len(oof)-1)
# 或者用更简洁的随机选元素方式
# ooo = random.choice(oof)

def taskslist():
    turtle.goto(-200,190)
    turtle.color('green')
    turtle.write('TASKS TO COMPLETE')
    y = 179
    turtle.hideturtle()
    for task in tasks:
        turtle.goto(-200, y)
        turtle.write(task)
        y -= 20

内容的提问来源于stack exchange,提问作者6969

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:56:29