# Lesson 9: Starry Night
This lesson learns are based on Python book page 90-97
# Tools
https://trinket.io/ (opens new window)
# Lesson Leans
# Random Package
- learn how to use a Random number in Python!
from random import randint
print(randint(1,10))
1
2
3
2
3
from random import randint
for i in range(10):
print(randint(1,10))
1
2
3
4
2
3
4
# Task
- try to run this code 3 times.
# Seed
- However, you might see everytime you run the code again. The random number would change everytime. We can use Seed number for fixed sequence.
- Seed can be anything. For instance, it can be number or string.
import random
from random import randint
random.seed(555)
for i in range(10):
print(randint(1,10))
1
2
3
4
5
6
7
2
3
4
5
6
7
output
8
1
10
7
6
2
10
4
7
5
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
import random
from random import randint
random.seed("some string")
for i in range(10):
print(randint(1,10))
1
2
3
4
5
6
7
2
3
4
5
6
7
output
6
8
7
6
5
7
5
9
10
4
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Random from List
import random
from random import choice
random.seed(555)
possible_point_stars = [5,7,9,11]
for i in range(10):
print(choice(possible_point_stars))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
output
9
5
11
9
9
5
11
7
9
7
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Starry Night
# Previous code
main.py
from turtle import *
my_turtle = Turtle()
def star(n_point, line_color, fill_color):
angle = 180 - (180/n_point)
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
for i in range(n_point):
my_turtle.forward(50)
my_turtle.right(angle)
my_turtle.end_fill()
def move_pen_to(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
def move_pen_forward(distance):
my_turtle.penup()
my_turtle.forward(distance)
my_turtle.pendown()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Start coding the Starry Night!
main.py
from turtle import *
import random
from random import choice,randint
random.seed(555)
my_turtle = Turtle()
my_turtle.speed(0)
def star(n_point, line_color, fill_color):
angle = 180 - (180/n_point)
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
for i in range(n_point):
my_turtle.forward(50)
my_turtle.right(angle)
my_turtle.end_fill()
def move_pen_to(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
def move_pen_forward(distance):
my_turtle.penup()
my_turtle.forward(distance)
my_turtle.pendown()
possible_star_points = [5,9,7,11,13,15,17]
for i in range(10):
x = randint(-150,150)
y = randint(-150,150)
star_point = choice(possible_star_points)
move_pen_to(x,y)
star(star_point, "black", "black")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
output

# Coloring
main.py
from turtle import *
import random
from random import choice,randint
random.seed(555)
my_turtle = Turtle()
my_turtle.speed(0)
def star(n_point, line_color, fill_color):
angle = 180 - (180/n_point)
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
for i in range(n_point):
my_turtle.forward(50)
my_turtle.right(angle)
my_turtle.end_fill()
def move_pen_to(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
def move_pen_forward(distance):
my_turtle.penup()
my_turtle.forward(distance)
my_turtle.pendown()
possible_star_points = [5,9,7,11,13,15,17]
line_colors = ["#65B667","#B561C9","black"]
fill_colors = ["#DA6291","#D36527","#6569E6","#1D5474"]
for i in range(10):
x = randint(-150,150)
y = randint(-150,150)
star_point = choice(possible_star_points)
line_color = choice(line_colors)
fill_color = choice(fill_colors)
move_pen_to(x,y)
star(star_point, line_color, fill_color)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
output
