Rainbow Challenge

rainbowIn this challenge we are going to use our coding skills to create some nice colour patterns. We will first look at the code given to create a rainbow effect to understand how it works. We will then adapt this script to create three other colour patterns.

Rainbow Effect


Try the code above. This code is using a few computing concetps such as:

  • A list to store the different colour codes,
  • Iteration using a for loop to loop through each colour in the list,
  • (x,y) coordinates to position the turtle on the screen.

List?


In Python, a list is used to save a collection of values. For instance, to save all the different days of the week we could declare a variable called “daysOfTheWeek” and assign a list as follows:

Based on this what do you think len(daysOfTheWeek) would return?
What’s about daysOfTheWeek[0]?
Or daysOfTheWeek[3] ?

Looping through a list


It is possible to access each value of a list one at a time using a for loop. Look at the following code to print the seven days of the week.

In our rainbow code we use a list to store all the different colours of the rainbow as follows:

(x,y) coordinates using turtle


quadrant-coordinates
On this trinket widget the turtle screen is 400 pixels wide. So:

  • the top left corner coordinates are (-200,200)
  • the top right corner coordinates are (200,200)
  • the bottom left corner coordinates are (-200,-200)
  • the bottom right corner coordinates are (200,-200)
  • the centre of the screen coordinates are (0,0)

Look at the canvas below to understand how (x,y) coordinates work:

Colour Pattern #1


Use and adapt the script above to recreate this colour pattern. The colour codes are given to you in the colour palette below.
color-pattern-1

Colour Hex RGB
#0099e6 (0,153,230)
#43e0ef (67,224,239)
#c3e970 (195,233,112)
#ffa9c2 (255,169,194)
#d297d9 (210,151,217)

Colour Pattern #2


Use and adapt your script to recreate this colour pattern. The colour codes are given to you in the colour palette below.
color-pattern-2

Colour Hex RGB
#ff0000 (255,0,0)
#d61a1a (214,26,26)
#a50000 (165,0,0)
#ddb200 (221,178,0)
#e7e497 (231,228,151)

Colour Pattern #3: Using a Random pattern


Use and adapt your script to recreate this colour pattern. The colour codes are given to you in the colour palette below.

However there is no colour pattern here. Each circle uses a random colour from the colour palette.

Check the code below used to randomly pick a colour from:

color-pattern-3

Colour Hex RGB
#ECECEC (236,236,236)
#2198d5 (33,152,213)
#0f8975 (15,137,117)
#0f6089 (15,96,137)
#000000 (0,0,0)

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges Tagged with: , , ,

Our Latest Book