【问题标题】:Python Turtle - Error with turtle.begin_fill() and turtle.end_Fill()Python Turtle - turtle.begin_fill() 和 turtle.end_Fill() 出错
【发布时间】:2015-04-13 14:47:45
【问题描述】:

我正在上高中计算机科学课,我似乎无法找到正确的放置/使用 turtle.begin_fill() 和 turtle.end_fill() 来为我正在绘制的形状着色。我试过移动这两个,但我一直收到这个错误:

TypeError: begin_fill() takes 1 positional argument but 2 were given

这是我的代码:

side_number = int(input("How many sides should the polygon have?"))
side_length = int(input("How long should the sides be?"))
side_color = (input("What color should the sides be?"))
fill_color = (input("What color should fill the shape?"))
import turtle
wn = turtle.Screen()
john = turtle.Turtle()
john.color(side_color)
for i in range(side_number):
    john.begin_fill(fill_color)
    john.forward(side_length)
    john.left(360/side_number)
    john.end_fill(fill_color)

【问题讨论】:

  • ...turtle.begin_fill 不接受任何参数(隐式的第一个 self 参数除外)。你期待fill_color 做什么?
  • 用颜色填充绘制的形状。我是否误解了它的功能是什么?我正在使用的教程没有解释 begin.fill 和 end.fill 的作用,但作业要求使用它们。我将如何用颜色填充形状?
  • begin_fill 给解释器发信号,“嘿,我要开始画一个形状了”,end_fill 给解释器发信号,“嘿,我画完了形状,请填写现在”。只需使用这两个就足够了,前提是您事先指定填充颜色并在正确的时间调用它们,如我的帖子中所述
  • @RialJohnson 是的,可能。您为什么不尝试阅读我刚刚链接的文档?
  • @Kevin 谢谢你的解释解决了很多困惑!

标签: python colors turtle-graphics


【解决方案1】:

begin_fillend_fill 不接受任何参数。您可以将填充颜色指定为turtle.color 的第二个参数。此外,begin_fillend_fill 应该在循环之外,因为它们应该每个多边形调用一次,而不是每行调用一次。

side_number = int(input("How many sides should the polygon have?"))
side_length = int(input("How long should the sides be?"))
side_color = (input("What color should the sides be?"))
fill_color = (input("What color should fill the shape?"))
import turtle
wn = turtle.Screen()
john = turtle.Turtle()
john.color(side_color, fill_color)
john.begin_fill()
for i in range(side_number):
    john.forward(side_length)
    john.left(360/side_number)
john.end_fill()

【讨论】:

  • 谢谢,它现在很有意义,可以做我需要做的事情!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 2018-07-28
  • 1970-01-01
相关资源
最近更新 更多