More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Code Maintainability Q&A

Question 1[4 marks]



The two Python scripts above were created by an IT manager of a school and are performing exactly the same task: to generate a username based on the student’s firstname, lastname and year group. Compare both of these scripts and identify what was done to the code to make it easier to understand, maintain and re-use in future projects?




Question 2[2 marks]

The IT manager would like to make this program more robust by adding a validation check on the year group. The school only has students in year 7 to year 13. How could the IT manager change the code above to make sure the user can only enter a value between 7 and 13 for the year group?




Question 3[4 marks]

The IT manager would like to test his new validation check on the year group. Identify 4 pieces of test data that the IT manager should use to perform a normal/valid test, an invalid test, a boundary test and an erroneous test?

Normal/Valid Test Data:
Invalid Test Data:
Boundary Test Data:
Erroneous Test Data:



The Marauder’s Map Coding Challenges

Welcome to Hogwarts, school of witchcraft and wizardry! For this set of challenges, you will be helping the famous wizard, Harry Potter, to recover all of his magical possessions.

Effectively, it would seem that a malevolent wizard did cast a spell on Harry Potter’s belongings. As a consequence, twelve of Harry Potter’s favourite magical objects have been buried on the school premises, within or around Hogwarts Castle.

Harry found a note on his bed containing 12 encoded messages that look like some kind of magical algorithms. He believes that he could use Professor Dumbledore’s Marauder’s Map to help him locate his 12 magical objects. However, Harry needs your help to decode these algorithms. Will you be able to use the Marauder’s Map and decode the 12 algorithms to locate all of Harry Potter’s magical artefacts?

The Marauder’s Map Coding ChallengesOpen in New Window
The Marauder’s Map Coding ChallengesOpen in New Window

The Moroccan Spices Box Set Puzzle

In the heart of the medina of Marrakech, you will find one of the largest and most colourful souks in Morocco. In this souk, you will find merchants selling traditional clothing, multicoloured carpets, pottery, jewellery, antiques, lanterns, spices, etc. On the spice square, Halim, a merchant of Moroccan spices, would like to sell different box sets of jars containing various spices. Halim has a collection of 10 jars and would like to create three box sets of exactly 250g each.

Could you help Halim create these three box sets using the following 10 jars. Each set must contain exactly 250g of spices. (Drag and drop the following jars to create three groups)

View Solution
There are more than one solution to this puzzle. Here is one possible solution:

Python Challenge

To help Halim to create similar box sets in the future, we would like to create a Python algorithm that suggests a possible solution for any given set of jars.

We will use a list to store the collection of jars available. For instance, using the current set of 10 jars, our list would be as follows:
jars = [150,20,20,10,80,130,110,90,100,40]

The aim of this challenge is to use a backtracking / recursive algorithm to work out a possible solution for this puzzle by creating three lists of jars, using the values from the above jars list to create 3 box sets of exactly 250g each.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

HTML and CSS Quizzes

Do you know your HTML tags used to create a webpage? Do you know how to use CSS to customise the look and feel of all the elements of this page? See how many HTML tags and how many CSS properties you can list in under 5 minutes by completing the following two quizzes.

To learn more about HTML and CSS you can use the tutorials from w3schools:

Tagged with: ,

Standard Deviation Algorithm

In statistics, given a set list of numbers, the standard deviation is a measure of the amount of variation or dispersion within the set.

A low standard deviation indicates that the values tend to be close to the mean value of the set.
A high standard deviation indicates that the values are spread out over a wider range.

The formula to calculate the standard deviation for a given set of numbers is as follows:

Where x represents the mean value of the set and n represents the number of values in the set.

Your task is to design an algorithm for a function called standardDeviation() that takes one parameter: a list of integer values. The list can be of any length. Your function will calculate and return the standard deviation for this given list.

Python Code

We have started the code for you by creating a function called inputList() that lets the user input any list of numbers, one number at a time. Your task is now to add the code for the standardDeviation() function.

Test Plan

Test # Input Values Expected Output Actual Output
#1 7,14,9,21,32,15 8.3199893162325
#2 23,101,52,48,107 32.480147782915
#3 10,40 15
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

The Jorvik Viking Settlement​

From around A.D. 800 to the 11th century, a large number of Scandinavians Vikings left their homelands to seek their fortunes on the east coast of England. These seafaring warriors ransacked different coastal sites from Northembria to East Anglia. In 866, they invaded the city of York which, after being renamed Jorvik, became their capital.​

​Most of their constructions was made of wood, hence there are not so many vestiges left of their occupation. Vikings invaded the East coast of England after crossing the North Sea on long wooden ships. One of their traditions was to bury these ships in the ground to create a burial site for their dead.​

While excavating a site under the Cathedral of York, a team of archaeologists have recently found a complex system of 12 locking compartments, carved directly in stone. They believe each compartment contains a Viking shield and that each shield has a symbol engraved on it. By accessing these 12 symbols, they believe they could reveal the location of a ship burial site where Ragnar Lothbrok, one of the most famous Viking warrior, is buried alongside his undiscovered treasure of inestimable value.​

Each locking mechanism consists of two sliders. For each mechanism, a diagram has also been carved in the stone which, we believe, provides the key to unlock the compartment. It is essential to open these compartments by finding the correct position of the sliders, as trying to break the mechanism itself to open the compartment would damage its content and would prevent the archaeologists from revealing the code engraved on each shield.​

The team of archaeologists is asking your help to help them unlock the 12 compartments and reveal the location of Ragnar Lothbrok’s treasure!​
The Jorvik Viking SettlementOpen in New Window
The Jorvik Viking SettlementOpen in New Window

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Countries of Africa

Can you name all of the 54 countries of Africa?

In this challenge we will write a Python script to create a quiz where we ask the user to guess as many African countries as possible. The player will score one point per correct country.

Using a list in Python

Our Python program will use a list to store all 54 countries.

countries = [“Algeria”, “Angola”, “Benin”, “Botswana”, “Burkina Faso”, “Burundi”, “Cabo Verde”, “Cameroon”, “Central African Republic”, “Chad”, “Comoros”, “Ivory Coast”, “Djibouti”, “Democratic Republic of the Congo”, “Egypt”, “Equatorial Guinea”, “Eritrea”, “Eswatini”, “Ethiopia”, “Gabon”, “Gambia”, “Ghana”, “Guinea”, “Guinea-Bissau”, “Kenya”, “Lesotho”, “Liberia”, “Libya”, “Madagascar”, “Malawi”, “Mali”, “Mauritania”, “Mauritius”, “Morocco”, “Mozambique”, “Namibia”, “Niger”, “Nigeria”, “Republic of the Congo”, “Rwanda”, “Sao Tome & Principe”, “Senegal”, “Seychelles”, “Sierra Leone”, “Somalia”, “South Africa”, “South Sudan”, “Sudan”, “Tanzania”, “Togo”, “Tunisia”, “Uganda”, “Zambia”, “Zimbabwe”]

To check how many items (countries) are in the list, we can use the len() function:

numberOfCountries = len(countries)
print("There are " + str(numberOfCountries) + " countries in Africa!")

To check if a country is in the list we can use the keyword in as follows:

if "Kenya" in countries:
   print("Kenya is in Africa!")

To remove a country from the list, we can use the remove() function. For instance is the user has named a country which is in the list, we can remove this country from the list.

if "Kenya" in countries:
   countries.remove("Kenya")

Flowchart

Below is the full flowchart for our Python quiz: (Click on flowchart to zoom in)

Python Code

We have started the code, you task is to complete the code in the trinket window below:

Extension Task

Amend this code to give the player three lives. For each invalid answer, the player should lose a life. The game should stop and display a game over message when the player has found all 54 countries or when they have lost their 3 lives.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Let’s Build a Castle

In this challenge we are going to build different medieval castles by customising some Python code.

Our first castle is already made for us and looks as follows:

You can see, in the trinket window below, the Python code used to create this castle.

The code uses a range of pre-defined functions to draw the different building blocks of the castle (walls, towers, doors, flags and loopholes). These functions can be accessed in the extra file (tab) called shapes.py. However for the purpose of this challenge, you will not need to access the code of these functions.

The main functions that you will have to reuse to cutomise the look and feel of your castle are:

  • drawWall()
  • drawTower()
  • drawDoor()
  • drawLoophole()
  • drawFlag()

Each of these functions is using some parameters to change the position, width and height of the different building blocks and to change the colour of the flag. Let’s investigate these functions and their parameters.

drawWall()drawTower()drawFlag()drawDoor()drawLoophole()(X,Y) Coordinates?Colour Codes?
The drawWall() function uses the following 6 parameters:

So the following code would be used to create a wall at position (-150,0). The wall would be 300 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawTower() function uses the following 6 parameters:

So the following code would be used to create a tower at position (-100,0). The tower would be 50 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawFlag() function uses the following 6 parameters:So the following code would be used to create a flag at position (120,120). The flag would be 30 pixels wide, 40 pixels high and be of a red colour.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a door at position (-100,0). The door would be 50 pixels wide, 150 pixels high.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a loophole at position (-100,0). The loophole would be 10 pixels wide, 30 pixels high.

(X,Y) Coordinates?


The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high.
Look at the canvas below to understand how (x,y) coordinates work:


Colour Codes


#FF0000#FFFF00#0000AA#00AA00

Python Code

Here is the Python code to create the castle.

Your Task

Your task is to edit the code from line 16 to 27 to re-create the following castles. To do so you will have to tweak the parameters of the functions used to create the castle. You may also need to remove or add some extra lines of code to add extra elements (towers, flags, loopholes, etc…)

Extension Task

Access the code in the shapes.py file (tab).
Create two more functions as follows:

  • drawSun() to draw a yellow circle at a given (x,y) set of coordinates.
  • drawTree() to draw a tree at a given (x,y) set of coordinates.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with:

Python Turtle Race

In this challenge we will create a Python script to complete a Turtle race with four turtles racing across the screen.

We have started the code for you to set up the pitch of grass. You will complete this project step by step in order to:

    Add a start line to the left of the screen,
    Add a finish line to the right of the screen,
    Add four turtles on the start line,
    Animate the turtles to race across the screen,
    Find out which turtle is first to cross the finish line

Here is the Python code that you will need to complete:

Step 1: Start lineStep 2: Finish lineStep 3: Adding TurtlesStep 4: Race Time!Step 5: And the winner is...

Step 1: Adding a start line


To draw the start line as a vertical line on the left hand side of the screen, we will need to use the following (x,y) coordinates to tell the computer where does the start line starts and ends:

You will can add the following code to the above trinket to draw the start line:

#Let's draw the start line
myPen.penup()
myPen.goto(-160, 150)
myPen.pendown()
myPen.goto(-160, 0)

Step 2: Adding a finish line


It’s now your turn to use very similar code, this time to draw the finish line using the following (x,y) coordinates:

View Solution!

Step 3: Adding four turtles on the start line

We are now going to add the four competitors on the start line. We will have four turtles that we will need to position using the following (x,y) coordinates:

Here is the code to add the red turtle at position (-180, 140):

#Let's add our turtles
redTurtle = turtle.Turtle()
redTurtle.shape('turtle')
redTurtle.color('red')
redTurtle.pensize(2)
redTurtle.penup()
redTurtle.goto(-180, 140)
redTurtle.pendown()

Can you add some more lines of code to add 3 more turtles using the following coordinates:

  • Green Turtle at position (-180,110)
  • Blue Turtle at position (-180,80)
  • Yellow Turtle at position (-180,50)
View Solution!

Step 4: Animating the turtles to race across the screen


It’s now time for our turtles to race across the screen. To do so we will use an infinite while loop. Within each iteration of the loop, we will move each turtle forward by a randomly generated number of pixels between 1 and 8.

Here is the code to use for our infinite loop…

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))

Step 5: And the winner is…

As you can see with step 4, the four turtles carry on racing even after crossing the finish line. They even disappearing off screen!

Our aim is now to check, within each iteration of the while loop, to see if one of the four turtles has reached the finish line. To do so, we will need to check the x coordinate of each turtle. A turtle with an x coordinate greater than 180 has crossed the finish line!

Here is the final code for our racing loop!

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))
  
  if redTurtle.xcor()>180:
    print("Red Turtle wins!")
    break
  elif greenTurtle.xcor()>180:
    print("Green Turtle wins!")
    break
  elif blueTurtle.xcor()>180:
    print("Blue Turtle wins!")
    break
  elif yellowTurtle.xcor()>180:
    print("Yellow Turtle wins!")
    break
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Crenellation Challenge

In a medieval castle, the crenelation (a.k.a battlement) is a parapet at the top of a wall that has regularly spaced squared openings for archers to shoot through.

In this challenge, we are going to use Python Turtle to create repeating patterns such as the crenellation of a medieval castle.

First, let’s investigate how to draw a horizontal line using python Turtle:

Task #1:

Your task is to edit the above code to create a single crenellation pattern as follows: (Instead of a horizontal line going across the screen).

To do so you will need to use a few Python Turtle instructions including:

  • myPen.forward(20)
  • myPen.left(90)
  • myPen.right(90)
View Solution!

Task #2: Repeating Pattern


In order to repeat this crenellation pattern to go across the screen, we will use a for loop.

for i in range (0,10):
    myPen.left(90)
    myPen.forward(15)
 
    myPen.right(90)
    myPen.forward(20)

    myPen.right(90)
    myPen.forward(15)

    myPen.left(90)
    myPen.forward(20)

Task 3: Alternative Crenellation Patterns…

Your task is to tweak the above code to recreate the following repetitive patterns…



unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: