【问题标题】:How to pass variable to function如何将变量传递给函数
【发布时间】:2021-09-30 15:36:02
【问题描述】:

我不知道我的代码哪里出了问题,我正在尝试一切:(

这是代码,感谢大家的帮助! :)

import turtle

def main():
    print("Project 1 by Amanda Basant")

main()

def draw_filled_square(turtle,size,color):
    turtle.fillcolor(color) 
    turtle.begin_fill()

    for i in range(4):
           turtle.forward(size)
           turtle.left(90)
    
turtle.end_fill()

def draw_picture():
    window = turtle.Screen()
    amanda = turtle.Turtle()
    amanda.up()
    amanda.goto(0,0)
    amanda.down()

    draw_filled_square(amanda,300,"blue")
    draw_filled_square(amanda,300,"green")

draw_picture()

我想最终画出enter image description here这个。我解决了我最初遇到的问题。我可以在盒子上写字母,但我现在在如何填满盒子和和乌龟一起跑方面很挣扎。有谁知道为什么这些盒子不会填满?

【问题讨论】:

  • 你现在遇到了什么错误?
  • 你的缩进看起来不对,我认为for 循环应该嵌套在你的draw_filled_square 函数中?
  • 我收到一个错误,说尺寸未定义,让我试试
  • 我嵌套了 for 循环,现在我收到一条错误消息,提示“未定义蓝色”
  • 简单的blue会变成变量,使用引号,"blue"表示字符串。绿色也一样。

标签: python turtle-graphics python-turtle


【解决方案1】:

没有填满方框的原因是turtle.end_fill() 位于raw_filled_square() 函数之后,而不是函数的最后一行。你没有得到两个盒子的原因是你在另一个上面画一个。让我们稍微修改一下这段代码,让它从你想要的图像中画出方框:

from turtle import Screen, Turtle

def main():
    print("Project 1 by Amanda Basant")

    screen = Screen()

    draw_picture()

    screen.exitonclick()

def draw_filled_square(turtle, size, color):
    turtle.fillcolor(color)
    turtle.begin_fill()

    for _ in range(4):
        turtle.forward(size)
        turtle.left(90)

    turtle.end_fill()

def draw_picture():
    amanda = Turtle()

    for _ in range(2):
        draw_filled_square(amanda, 300, "green")
        amanda.right(90)
        draw_filled_square(amanda, 300, "blue")
        amanda.right(90)

main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 2011-06-21
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多