More results...

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

Computing Terminology

Check your computing terminology by joining the dominoes below: (Click on picture to start).

dominoes

Computing TerminologyOpen Domino Activity
 

Yes or No!

yes-no

Learning Objectives


When your computer prorgam is asking questions to the end-user, the end-user may not answer these questions the way you expected them to. This may cause your program to not work properly. Ideally a good program should use some form of validation checks to ensure that the user is providing their answers in the correct format.

Let’s look at the following code that asks a simple Yes/No question:

answer = input("Are you over 16?")
if answer == "Yes":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "No":
   print("You are too young to learn how to drive a car.")

What would happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?

Now let’s look at how we could improve this code further:

Solution #1Explanations
answer = input("Are you over 16?").upper()
if answer == "YES":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
   print("You are too young to learn how to drive a car.")
Check the code, did you notice that we added .upper() at the end of line 1?
This will convert the user input into capital letters.
Then, within our if statements, we can compare the user input with “YES” and with “NO”.

What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #2Explanations
answer = input("Are you over 16?").upper()
if answer == "YES":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
   print("You are too young to learn how to drive a car.")
else:
   print("Sorry your answer is not recognised. Restart the program and make sure you answer with the word Yes or the word No.")
By adding an else statement we can give some feedback to the end-user to tell them why their answer is not valid and how they need to answer the question next time

What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #3Explanations
answer = ""
while (answer!="YES" and answer!="NO"):
  answer = input("Are you over 16? (Yes or No)").upper()

if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
Using a while loop, we keep asking the question up until the user has typed a valid answer (Yes or No).
What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #4Explanations
def askYesNoQuestion(question):
  YesNoAnswer = ""
  while (YesNoAnswer!="YES" and YesNoAnswer!="NO"):
    YesNoAnswer = input(question).upper()
  return YesNoAnswer

answer = askYesNoQuestion("Are you over 16? (Yes or No)")
if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
By creating our own askYesNoQuestion() function we can easily reuse this function every time we want to ask a YesNo question! In our main program, it is as easy as using an input command. It is however a lot more effective because it makes sure the user gives a valid answer!
Solution #5Explanations
def askYesNoQuestion(question):
  YesNoAnswer = input(question).upper()
  if YesNoAnswer == "YES" or YesNoAnswer == "NO":
     return YesNoAnswer  
  else:
     return askYesNoQuestion(question)

answer = askYesNoQuestion("Are you over 16? (Yes or No)")
if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
This time our askYesNoQuestion() function is not using a while loop but it is using recursion: It’s calling itself. Check on line 6 for the recursive call to itself. And it will keed doing so until it receives a valid answer.

Extension Task:


These different approaches to validate a user input only work for “Yes” / “No” questions.
Would you be able to adapt these to make them work for:

  • Multiple answers questions? e.g. Which Science subject do you prefer: Biology, Physics, Chemistry or Computer Science?
  • Number based questions? e.g. Which year group are you in: Only accept a value between 1 and 13. (Tip you will need to use < and > comparison operators to validate the user input.)
Tagged with: , , ,

Flags of the World

We are trying to build a program that will ask the end-user to choose a flag amongst six flags. The program will then try to guess the selected flag by asking a maximum of two questions.

Look at the flowchart below used to describe our algorithm: (Click on flowchart to open in new window)
flags of the world flowchart

Challenge #1


Analysing this flowchart, can you guess the country corresponding to these six flags? (Type your answer below each flag).

Flag-Tanzania Flag-Turkey Flag-Luxembourg
Flag-Ghana Flag-Sweden Flag-Chad

Challenge #2

Using Flowchart Studio, design a similar flowchart used for the computer the guess the correct flag amonst these six new flags. You will need to ask different questions.

Flag-Jamaica Flag-China Flag-Iceland
Flag-Colombia Flag-Cuba Flag-Denmark

Challenge #3


Can you tweak the algorithm from the previous task to get the computer to guess the correct flag amongst all twelve flags, using a maximum of three consecutive questions?

Tagged with: ,

Fireworks Display

fireworks

Coding is an Art


Let’s first whatch this video clip which shows how a computer program can be used to create a large scale, live and interactive fireworks display:
Watch video clip

Fireworks Code


Inspired by this application of computer science, we have decide to create our own firework display using a Python script.

For this challenge we are using the processing library which is used to draw on the screen and to refresh the screen based on a frame rate. This is what we need to create our fireworks animation, based on a 24 frames per second.

Your task:


Analyse the code above. Change some of the parameters to see the impact on the aimation. Customise your fireworks further based on your preferences.

Interactive Fireworks


Let’s make an interactive fireworks display that responds to the user typing on the keyboard.
To test this script make sure you click on the fireworks display first (while it’s running). Then you will be able to interact with it by pressing any key on the keyboard:

Tagged with: , ,

My daily routine

daily-routine

Learning Objectives


When you write lines of code, there are three ways you can control the order these lines will be executed by the computer:

  1. Sequencing: This means that the computer will run your code in order, one line at a time from the top to the bottom of your program. It will start at line 1, then execute line 2 then line 3 and so on till it reaches the last line of your program.
  2. Selection: Sometimes you only want some lines of code to be run only if a condition is met, otherwise you want the computer to ignore these lines and jump over them. This is achieved using IF statements. e.g. If a condition is met then lines 4, 5, 6 are executed otherwise the computer jumps to line 7 without even looking at line 4,5 and 6.
  3. Iteration: Sometimes you want the computer to execute the same lines of code several times. This is done using a loop. There are three types of loops: For loops, while loops and repeat until loops. That’s handy as it enables you not to have to copy the same lines of code many times.

Let’s Get Coding


For this task we are going to write a program that uses sequencing, selection and iteration all in the same program! Our program will be used to print our daily routines on screen over a full week. Look at the code below and try to make sense of how it works. Can you spot where and how sequencing, iteration and selection are used in this code?

Your Task


Your task is to tweak this code to customise your daily routines. Add to it some of the activities or clubs you go to. For instance:

  • Are you going swimming on Tuesdays?
  • Do you play football on Saturdays?
  • Do you go to a Youth Club on Thursdays?
  • Do you play music on Fridays?
  • Is Friday Fish and Chips day?
Tagged with: , ,

Sweet Shop

sweetsHave you ever been in a sweet shop to buy sweets? For this challenge we are going to spend £5 in a sweet shop hence we need to find out how many sweets we can afford. We will want to pick and mix sweets until we have spent all our money.

To help us buy our sweets we are going to write a program that will help us decide how many sweets we can afford while allowing us to pick and mix different types of sweets.

Here are the sweets available in the shop:
sweet-shop-price-list

Here are the main steps of our program which will:

  1. Display a price list of all the sweets available in the shop,
  2. Ask the end-user how much they would like to spend,
  3. Ask the user which sweet they would like to buy and how many of them they would like (A to E),
  4. Allow the user to enter X (instead of the A to E letter for a sweet) to stop buying more sweets,
  5. Check whether the use can afford these and if they can, calculate and display how much money they have left,
  6. Repeat steps 3 to 5 for as long as the user has some money left.

Now let’s see how a flowchart can help us describe these steps further:
sweet-shop-flowchart

Your task:


Use the above flowchart to complete the code below:

unlock-access

Solution...

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

The window cleaner’s quote

window-cleanerA window cleaner uses the following pricing policy to calculate how much to charge for cleaning all the windows of his customer’s dwelling. This pricing policy is based on the number of windows that need to be cleaned and works as follows:

  • All quoted prices include a fixed call out fee of £10,
  • Then, the first five windows are charged at £2 each,
  • The next five windows are charged at £1.50 each,
  • Any additional windows are charged at £1 each.

Your task is to write a computer program that prompts the end-user to enter the number of windows of their dwelling. The program will then calculate the quoted price using the pricing policy described above and display it to the end-user.

Test Plan


Now that you have implemented the cost calculator for the house cleaner, you are going to test your code by completing the following tests: see test plan below. For each test compare the outcome of your program with the expected outcome. If these are different then you’ll need to review and tweak your code.

Test # Input values Expected outcome Actual outcome
1 Number of windows: 3 Cost: £16
2 Number of windows: 5 Cost: £20
3 Number of windows: 7 Cost: £23
4 Number of windows: 9 Cost: £26
5 Number of windows: 10 Cost: £27.50
6 Number of windows: 11 Cost: £28.50
7 Number of windows: 13 Cost: £30.50
8 Number of windows: 15 Cost: £32.50

Extension Task:


At the beginning of the year, the house cleaner wants to attract new customers by offering a 10% discount to all quoted prices.

Adapt your code to include a 10% discount in the quoted price.

Solution



unlock-access

Solution...

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

Gradient Animation

color-gradientIn this challenge we are going to create some animated gradients by progressively changing the colour of the screen from one colour (e.g. red) to another (e.g. yellow).

RGB Colour Codes


Did you know that every colour on the screen can be represented using an RGB code (Red, Green, Blue) code. This code consists of three numbers between 0 and 255, indicating how much red, green and blue are used to recreate the colour.

For instance the RGB code for:

  • Red is (255,0,0)
  • Green is (0,255,0)
  • Blue is (0,0,255)
  • Yellow is (255,255,0)
  • Orange is (255,165,0)

Check the following RGB Color picker to see how RGB codes work:

Learning Objectives


By completing this code we will investigate how for loops work to iterate through the code a fixed number of times.

We will use an increment in our loop that will go up (increment) by 1 after each iteration, to count from 0 to 255. We will see that we can also use a negative step to count down from 255 to 0.

Gradient #1:


Our first animation will be based on the following gradient:
color-gradient-1

It will consist of:

  • 256 frames to shade from red (255,0,0) to yellow (255,255,0)
  • + another 256 frames to shade from yellow (255,255,0) back to red (255,0,0)
  • It will repeat the above steps 3 times so in total will consist of (256 + 256) x 3 = 1536 frames!

Check the code below: If this code does not work in Internet Explorer, try to access this webpage using Google Chrome.

Gradient #2: From red (255,0,0) to blue (0,0,255) to red (255,0,0)


color-gradient-2

This code is slightly different as we want the red code to decrement from 255 to 0 while the blue code increments from 0 to 255. Check how it’s done in the code below:

Gradient #3: From cyan (0,255,255) to magenta (255,0,255) to cyan (0,255,255)


color-gradient-3

Tweak the code in one of the trinkets above to implement this gradient.

Gradient #4: From cyan (0,255,255) to yellow (255,255,0) to cyan (0,255,255)


color-gradient-4

Tweak the code in one of the trinkets above to implement this gradient.

Gradient #5: From green (0,255,0) to yellow (255,255,0) to magenta (255,0,255) to cyan (0,255,255) to green (0,255,0)


color-gradient-5

Tweak the code in one of the trinkets above to implement this gradient.

Gradient #6: From black (0,0,0) to white (255,255,255) to black (0,0,0)


color-gradient-6

Tweak the code in one of the trinkets above to implement this gradient.

unlock-access

Solution...

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

My Charts

chartFor this challenge we are going to use Python turtle to create some charts: Column charts, line charts and radar charts.

First we need a data set. We will use the following data that shows the market share of the four most popular browsers in 2015.

Web-browser Percentage
Chrome 44%
Safari 20%
IE / Edge 14%
Firefox 12%
Other 10%

Your Challenge


Using Python Turtle, create the following charts:
chart-column

chart-line

chart-radar

Python Turtle


Looking at the following code can you explain the purpose of each Python Turtle instructions:

  • myPen.color(“red”)
  • myPen.forward(100)
  • myPen.right(90)
  • myPen.left(45)
  • myPen.penup()
  • myPen.pendown()
  • myPen.goto(0,0)
  • myPen.beginfill()
  • myPen.end_fill()
  • myPen.begin_fill()
  • myPen.write(“Hello World”, None, “center”, “16pt bold”)

(x,y) coordinates using Python Turtle?


quadrant-coordinatesOn this trinket widget the turtle screen is 400 pixels wide by 400 pixels high. 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:

Challenge #1: Complete the code


We have started the code by creating a function called drawColumnChart(). This function takes five parameters – the values of the five columns to appear on the chart.

The code below shows you how to draw the first column. Your job consists of completing this code to draw all five columns.

Challenge #2: Line chart


Using a similar approach add a new function called drawLineChart() to draw a line chart using the same five values.
chart-line

Challenge #3: Radar chart


Using a similar approach add a new function called drawRadarChart() to draw a radar chart using the same five values.
chart-radar

Tagged with: ,

Penalty Shootout

soccerFor this challenge you are going to write a computer program where the user tries to score a goal against the computer.

The user will be asked where do they want to shoot and will have to choose one of the following five options:

  • TL: Top Left,
  • BL: Bottom Left,
  • M: Middle,
  • TR: Top Right,
  • BR: Bottom Right.

football-goal

The computer will act as the goal keeper and randomly choose one of these options too.

The program will decide if there is a goal or not by comparing the user’s choice with the computer option.

Complete the code


We have started the code for you. You need to complete it further:

Video Tutorial


Extension Task 1:


Create a second level where the computer can block up to two locations which are next to each other such as:

  • Bottom left and right left,
  • Bottom left and middle,
  • Top right and middle,
  • etc.

Extension Task 2:


Give the end-user a choice: do they want to shoot the penalty or be the goal keeper? Adapt your code to cater for both options.

Extension Task 3:


Create a program where the computer and the player take it in turn. The program adds up the scores and stops on a best of 5 scores. (e.g. 3-0, 4-1, 4-2, 5-3, 5-4)
unlock-access

Solution...

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