【问题标题】:TypeError: type object argument after * must be an iterable, not intTypeError: * 之后的类型对象参数必须是可迭代的,而不是 int
【发布时间】:2016-12-16 16:07:08
【问题描述】:

我正在创建一个 Turtle 程序,它将绘制一棵圣诞树和一些小玩意。我希望小玩意有随机的颜色,并去圣诞树上的随机点。这是我的代码:

turtle.goto(random.randint(1,8)),(random.randint(1,8))

但是当我运行程序时,出现了这个错误:

TypeError: * 之后的类型对象参数必须是可迭代的,而不是 int

我该如何解决这个问题?

【问题讨论】:

  • gotorandint()中的括号有问题
  • 你能显示你的函数的定义goto吗?

标签: python typeerror turtle-graphics


【解决方案1】:

我不知道乌龟,但我最好的猜测是你的括号有问题:

turtle.goto(random.randint(1,8)),(random.randint(1,8))
#   Extra closing parenthesis  ^,^ extra opening

改成:

turtle.goto(random.randint(1,8), random.randint(1,8))

【讨论】:

    【解决方案2】:

    goto 采用 x 和可选的 y turtle.goto(x, y=None)

    如果我们有

    x = random.randint(1,8)
    y = random.randint(1,8)
    

    我们可以做到

    turtle.goto(x, y)
    

    或者一口气,留出一些空间以提高可读性和发现错误的额外机会,尽可能少用大括号

    turtle.goto( random.randint(1,8), random.randint(1,8) )
    

    您不需要在给goto 的值周围加上额外的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多