简体   繁体   中英

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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