星空背景
from turtle import *import randomscreen = Screen()screen.setup(1200, 800)screen.bgcolor('black')speed(0)penup()hideturtle()for _ in range(100):x = random.random()print('the x:', x)x = x * 1200 - 1200/2y = random.random() * 800 - 800/2r = random.random()g = random.random()b = random.random()d = random.randint(0, 3)goto(x, y)color(r, g, b)dot(d)done()
八大行星

课外练习
到处乱窜的小乌龟
#!/usr/local/bin/env python3# encoding: utf-8'''create a window and a turtlewhile the turtle is still in the window:generate a random number from 0 to 1if the number == 0:turn leftelse:turn rightmove the turtle forward 50'''import randomimport turtledef isInScreen(w, t):boundleft = - w.window_width() / 2boundright = w.window_width() / 2boundtop = w.window_height() / 2boundbottom = -w.window_height() / 2turtleX = t.xcor()turtleY = t.ycor()inscreen = Trueif turtleX > boundright or turtleX < boundleft:inscreen = Falseif turtleY > boundtop or turtleY < boundbottom:inscreen = Falsereturn inscreenwn = turtle.Screen()t = turtle.Turtle()t.shape('turtle')while isInScreen(wn, t):step = random.randrange(0, 2)if step == 0:t.left(90)else:t.right(90)t.forward(50)wn.exitonclick()

