TypeError:*后面的类型对象参数必须是iterable,而不是in

2024-10-05 10:38:17 发布

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

我正在创建一个海龟计划,将画一棵圣诞树和一些装饰品。我希望圣诞饰品有随机的颜色,去随机点在圣诞树上。这是我的代码:

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

但是,当我运行程序时,出现以下错误:

TypeError: type object argument after * must be an iterable, not int

我该怎么解决?


Tags: 代码程序颜色type错误random计划海龟
2条回答

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

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

goto接受一个x和一个可选的yturtle.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的值周围不需要额外的paraen。

相关问题 更多 >

    热门问题