I am learning Python right now, which is a programming language. I am also reading a book called
Coding for Kids: Python.
I use Thonny to edit and run my Python code as well as install third-party modules.
Turtle
In this code, I set a variable to 7. I used a for loop (for i in range(7)), and each time the variable goes down by 1. If the variable equals 0, the turtle turns red, stamps, and goes
forward by 50. If it equals 1, it turns orange, stamps, and goes forward by 50. It keeps going like that until 6, so
it makes a line of rainbow turtles.
import turtleturtle = turtle.Turtle()turtle.turtlesize(2, 2, 2)turtle.shape("turtle")turtle.penup()turtle.back(230)for i in range(7): turtle.forward(50) if i == 0: turtle.color("red") elif i == 1: turtle.color("orange") elif i == 2: turtle.color("yellow") elif i == 3: turtle.color("green") elif i == 4: turtle.color("light blue") elif i == 5: turtle.color("blue") elif i == 6: turtle.color("purple") turtle.stamp()
Flake
My dad helped me make this code. In it,
draw_wedge() is a function that calls itself.
import turtledef draw_wedge(pen, L, depth): if depth == 0: pen.forward(L) else: draw_wedge(pen, L / 3, depth - 1) pen.left(60) draw_wedge(pen, L / 3, depth - 1) pen.right(120) draw_wedge(pen, L / 3, depth - 1) pen.left(60) draw_wedge(pen, L / 3, depth - 1)if __name__ == "__main__": pen = turtle.Turtle() length = 250 pen.speed(500) pen.penup() pen.back(length) pen.left(60) pen.forward(length) pen.right(60) pen.pendown() for i in range(3): draw_wedge(pen, length, 4) pen.right(120) pen.ht()
Diamonds
This code uses the same draw_wedge() function to draw a cooler shape!