简体   繁体   English

如何将变量传递给 function

[英]How to pass variable to function

I can't figure out where I am going wrong with my code and I am trying everything:(我不知道我的代码哪里出了问题,我正在尝试一切:(

Here is the code and thank you to everyone helping: :)这是代码,感谢大家的帮助::)

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

I want to draw enter image description here this ultimately.我想在这里最终绘制输入图像描述 I fixed the initial problem I had.我解决了最初的问题。 I can do the letters on the box, but I am struggling bad on how to fill the boxes and run with the turtle now.我可以写盒子上的字母,但我现在在如何填满盒子和与乌龟一起跑方面苦苦挣扎。 Does anyone know why the boxes won't fill?有谁知道为什么盒子不会填满?

The reason that the boxes aren't filled is that turtle.end_fill() is after the raw_filled_square() function instead of being the last line of the function. The reason you're not getting two boxes is that you're drawing one atop the other.盒子没有被填满的原因是turtle.end_fill()raw_filled_square() function 之后,而不是 function 的最后一行。你没有得到两个盒子的原因是你在上面画了一个另一个。 Let's rework this code a little bit to make it draw the boxes from your desired image:让我们稍微修改一下这段代码,让它从你想要的图像中绘制方框:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM