Module 6 - Section 1 - Loops and Animation
6.1.2 - List of pairs
As we already know the position of the snowflake is a list of two numbers. If we want to draw 50 snow flakes we need 50 pairs of numbers. How can we create 50 pairs at once. Using a for loop. And where we can store them? In a list of course.
Create a new script in Thonny and save it as snowList (or whatever you like) inside your project's folder.
In the script area of Thonny write, or copy and paste the following commands:
import random
snow_list = []
for i in range(50):
x = random.randrange(10,690)
y = random.randrange(10,490)
snow_list.append([x,y])
print(snow_list)
Run the script. Every time it creates a list of 50 pairs of numbers which we are going to use as coordinates for our snowflakes.
How can we access the items of the list?
Try this in CLI area:
>>> print(snow_list[0])
or even
>>> snow_list[4]
How can we use these pairs to draw snowflakes?
If you haven't managed to create the script you can download it from here and save it inside your project's folder as snowList