More results...

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

Python Typing Text Effect

keyboard-typingIn this blog post we will revisit two of the key python functions: print() and input() in order to improve the user experience of our Python programs.

As you know, the print() function in Python is used to output a message on screen, whereas the input() instruction displays a message/question on screen and retrieves a user input that the user will type using their keyboard.

Our aim is to improve the display of text on the screen by adding a typing effect/delay.

To do so we will need to import two libraries: time and sys. We will then create two new functions called typingPrint() and typingInput() as follows:

import time,sys

def typingPrint(text):
  for character in text:
    sys.stdout.write(character)
    sys.stdout.flush()
    time.sleep(0.05)
  
def typingInput(text):
  for character in text:
    sys.stdout.write(character)
    sys.stdout.flush()
    time.sleep(0.05)
  value = input()  
  return value

We will then be able to use these two functions in our program instead of the usual print() and input() functions.

Finally, to improve the user experience further, we have also created a clearScreen() function which relies on importing the os library. This function can be used at anytime to clear the screen from previous outputs and inputs. It’s ideal when creating menu based systems, splash screes, quiz based games, two player games and so on… Here is the code for our clearScreen() subroutine.

import os
 
def clearScreen():
  os.system("clear")

Demonstration


Let’s see how to use our functions in small text-based program:

Tagged with:

What if planet Earth was made of Lego?

In this blog post we will use a Python script to work out how many lego bricks would be needed to build planet Earth!

To solve our problem we will use the following data:
planet-Earth-lego-brick

We will work out the number of lego bricks needed in several step using a sequencing algorithm: An algorithm consisting of instructions that are carried out (performed) one after another.
sequencing-label
We will use the following steps:

  1. Convert all measurement to the same unit (e.g. mm)
  2. Calculate the volume of planet Earth
  3. Calculate the volume of a lego brick
  4. Number of bricks needed = volume of planet Earth / volume of a lego brick

We will need to use the following two formulas:
volume-sphere-cuboid

The complete flowhchart for our algorithm:
what-if-planet-earth-was-made-of-lego-flowchart

Python Code


You can now code this algorithm using Python to reveal the answer to the initial question: how many lego bricks are needed to build planet Earth!

Number of lego bricks


If you have completed this algorithm, the final answer should be approximatively:
≈ 4.23 x 1026 (in standard form)

≈ 423,000,000,000,000,000,000,000,000 lego bricks!

Using subroutines


It is good practice to use subroutines whenever possible as these can then easily be reused in other programs. Your task is to adapt your code by defining a d using two functions as follows:

  1. Your first subroutine should be called getVolumeOfSphere(), take one parameter called radius and apply the relevant formula to calculate and return the volume of a sphere based on the given radius.
  2. Your second subroutine should be called getVolumeOfCuboid(), take three parameters called width, length and height and apply the relevant formula to calculate and return the volume of a cuboid based on the given dimensions.
unlock-access

Solution...

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

Lemon Drizzle Cake Recipe (HTML Task)

lemon-drizzle-cakeIn this blog post we are going to help Elian save his favourite cake recipe as a web page. Elian has already typed all the instructions for his lemon drizzle cake recipe but needs your help to format how the text appears on the page. To help him improve the look and feel of his page you will need to complete the six tasks listed below.

But first let’s look at Elian’s HTML page so far:

See the Pen
Lemon Drizzle Cake Recipe
by 101 Computing (@101Computing)
on CodePen.


To edit Elian’s code, you will need to click on the “Edit on codepen” button in the top right corner above.

Your Tasks


Task 1: HeadingsTask 2: Bullet pointsTask 3: ColoursTask 4: Formatting textTask 5: HyperlinksTask 6: Your turn

Task 1: Headings and Paragraphs


In order to give this page more structure we are going to add some headings, subheadings and paragraphs. We will do so using the following HTML tags:

<h1>Heading 1</h1>

<h2>Heading 2</h2>

<h3>Heading 3</h3>

<h4>Heading 4</h4>

<h5>Heading 5</h5>
<h6>Heading 6</h6>


<P>Paragraph</P>


Your task is to update Elian’s code by adding some <h1> tags for the main heading:

  • Lemon Drizzle Cake Recipe

And some <h2> tags for the subheading of the page:

  • About the Recipe:
  • Ingredients:
  • Recipe for the cake:
  • Recipe for the lemon drizzle:
  • Finding more recipes online:

All other pieces of text should be displayed as paragraphs using <p> tags.

Note that, within a paragraph, we can also force the text to go to the next line using the <br/> tag.
Watch Video Tutorial #1

Bullet Points Lists


In HTML, you can create a list of bullet points using both <ul> tags and <li> tags as follows.

<ul>
   <li>First Bullet point!</li>
   <li>Second Bullet point!</li>
   <li>Third Bullet point!</li>
</ul>

Your task consist of displaying all the ingredients using bullet points so that it appears on the web page as follows:

  • 225g unsalted butter
  • 180g caster sugar
  • 4 eggs
  • 225g self-raising flour
  • 1 lemon, zested
  • 2 lemons, juiced
  • 65g caster sugar

You can also use a similar approach to create a numbered list as opposed to a bullet point list. To do so, use an <ol> tag instead of the <ul> tag. You can for instance display the steps of the recipe using a numbered list:

  1. Pre-heat the oven to 180°C.
  2. Beat together the butter and caster sugar until creamy, then add the eggs, one at a time, slowly mixing through.
  3. Sift in the self-raising flour, then add the lemon zest and mix until well combined.
  4. Line a loaf tin (8 x 21cm) with greaseproof paper, then pour in the mixture and level the top with a spoon.
  5. Bake for 45 to 50 minutes.
Watch Video Tutorial #2

Adding Colours!


We are now going to change the text colour using the <font> tag.

For instance to make text appear in orange we can use the following code:

<font color="orange">This text will appear in orange!</font>

Notice the American spelling for the attribute “color” inside the font tag!

Your task is to use several <font> tags to make the information such as preparation time, cooking time, level of difficulty and number of servings standout on the page using the colours of your choice.
Watch Video Tutorial #3

Centred ,Bold, Italic and Underlined Text


You can use the <center>, <b>, <i> and <u> tags to make text appear in the centre, in bold, in italic or underlined.

<center>This text will appear in the centre!</center>
<b>This text will appear in bold!</b>
<i>This text will appear in italic!</i>
<u>This text will appear underlined!</u>

Adapt Elian’s code so that some of the key information from the recipe appear in bold or in italic.
Watch Video Tutorial #4

Adding hyperlinks


To add a hyperlink, you will need to use the <a> tag as follows:

<a href="https://www.101computing.net">Visit our computing blog!</a>

You can now add a hyperlink to the last paragraph to point to the BBC Good Food website. The URL of the page is: https://www.bbcgoodfood.com/
Watch Video Tutorial #5

Your Turn


Now that you know how to use the basic HTML tags needed to create a web page, add some more content to this webpage:

  • Add a section called “Tip of the day” (as a Heading 2). Add a paragraph to this section with the following text:
  • “To find out is your cake can come out of the oven, insert a thin skewer or knife into the centre of the cake. Your skewer/knife should come out clean. If it doesn’t you will need to leave the cake to bake for a bit longer.”

  • Find some other recipes online and create hyperlinks to these pages.
unlock-access

Solution...

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

The colours of the rainbow (HTML task)

rainbowIn this blog post we are going to help Asya complete her homework research task about rainbows. Asya has collated a range of interesting facts about rainbows and has decided to submit her work as an HTML page.

She has started her page but needs your help to format this page further using a range of HTML tags. To help her improve the look and feel of her page you will need to complete the six tasks listed below.

But first let’s look at Asya’s HTML page so far:

See the Pen
Rainbow Facts
by 101 Computing (@101Computing)
on CodePen.


To edit Asya’s code, you will need to click on the “Edit on codepen” button in the top right corner above.

Your Tasks


Task 1: HeadingsTask 2: Bullet pointsTask 3: ColoursTask 4: Formatting textTask 5: HyperlinksTask 6: Your turn

Task 1: Headings and Paragraphs


When creating an HTML, page it is good practice to include headings, eventually subheadings and paragraphs. We can do so using the following HTML tags:

<h1>Heading 1</h1>

<h2>Heading 2</h2>

<h3>Heading 3</h3>

<h4>Heading 4</h4>

<h5>Heading 5</h5>
<h6>Heading 6</h6>


<P>Paragraph</P>


Your task is to update Asya’s code by adding some <h1> tags for the main heading:

  • Rainbow facts!

And some <h2> tags for the subheading of the page:

  • Colours of the rainbow
  • What Makes a Rainbow?
  • Rainbow Myths
  • Find out more:

All other pieces of text should be displayed as paragraphs using <p> tags.
Watch Video Tutorial #1

Bullet Points Lists


In HTML, you can create a list of bullet points using both <ul> tags and <li> tags as follows.

<ul>
   <li>First Bullet point!</li>
   <li>Second Bullet point!</li>
   <li>Third Bullet point!</li>
</ul>

Your task consist of displaying the seven colours of the rainbow using bullet points so that it appears on the web page as follows:

  • Red,
  • Orange,
  • Yellow,
  • Green,
  • Blue,
  • Indigo,
  • Violet.
Watch Video Tutorial #2

Adding Colours!


We are now going to change the text colour using the <font> tag.

For instance to make text appear in red we can use the following code:

<font color="red">This text will appear in red!</font>

Notice the American spelling for the attribute “color” inside the font tag!

Your task is to use several <font> tags to display each colour of the rainbow in its own colour.
Watch Video Tutorial #3

Centred ,Bold, Italic and Underlined Text


You can use the <center>, <b>, <i> and <u> tags to make text appear in the centre, in bold, in italic or underlined.

<center>This text will appear in the centre!</center>
<b>This text will appear in bold!</b>
<i>This text will appear in italic!</i>
<u>This text will appear underlined!</u>

Adapt Asya’s code so that the myth “There’s a pot of gold at the rainbow’s end.” appears in the centre of the page, in bold, and in italic.
Watch Video Tutorial #4

Adding hyperlinks


To add a hyperlink, you will need to use the <a> tag as follows:

<a href="https://www.101computing.net">Visit our computing blog!</a>

You can now add a hyperlink to the last paragraph to point to the wikipedia page about rainbows. The URL of the page is: https://en.wikipedia.org/wiki/Rainbow
Watch Video Tutorial #5

Your Turn


Now that you know how to use the basic HTML tags needed to create a web page, add some more content to this webpage:

  • Find some cool facts about rainbows and add them to the existing sections or create a new sub-heading to add your new content.
  • Find some other webpages about rainbows and create hyperlinks to these pages.
unlock-access

Solution...

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

Standard Form Conversion Algorithm

standard-formStandard form (a.k.a. standard index form), is a system of writing numbers which is particularly useful when using either very large or very small numbers. It is based on using powers of 10 to express how big or small a number is.

Standard form is written in the form of a x 10n, where is a number between 1 and 10 (bigger than or equal to 1 but strictly lower than 10). For large numbers, n is a positive whole number, whereas for small numbers, n is negative whole number.

stars-galaxy

The standard form is for instance used by astronomers as the distance between stars is very high, resulting in numbers which are not easy to write down and memorise without using the standard form. For instance did you know that Alpha Centauri was the second nearest planet (after the Sun) to planet Earth. It is located 4.37 light-years away from the Sun. Considering that a light-year is the distance travelled by light in one year and knowing that light travels at 300,000 km per second we can work out the distance in km between Alpha Centauri and the Sun as follows:

4.37 light-years = 4.37 x 365.25 x 24 x 60 x 60 x 300,000 ≈ 41,400,000,000,000 km

In standard form:
4.14 x 1013 km

h2o-moleculeSimilarly, physicists and chemists use the standard form when using very small numbers. For instance, the size of a molecule of water (H2O) is about 0.27 of a nanometer.

0.27 nanometers = 0.00000000027 meters

In standard form:
2.7 x 10-10 meters

Programming Challenge


Our aim is to write a procedure that takes a number as a parameter and outputs this number in standard form. To do so, our procedure will be based on the following algorithm:
standard-form-conversion-flowchart

Python Code


Complete the python code using the trinket below:

Test Plan


All done? It’s now time to test your code to see if it works as expected.

Test # Input Values Description Expected Output Actual Output
#1 41400000000000 The distance (in km) between the Sun and Alpha Centauri 4.14 x 1013
#2 0.00000000027 The size (in meters!) of a molecule of water. 2.7 x 10-10
#3 5972000000000000000000000 The mass (in kg) of planet Earth 5.972 × 1024
#4 1898000000000000000000000000 The mass (in kg) of Jupiter 1.898 × 1027
unlock-access

Solution...

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

Find the Logic Error

Check the following pieces of code. The syntax of the code is correct and the programs do run. However, while testing these programs, you will notice that they do not always produce the expected output! This is because each program contains a logic error. Can you spot and fix these logic errors?

Program 1:Program 2:Program 3:Program 4:Program 5:Program 6:

Higher or Lower?


The aim of this program is to identify the highest number from 2 user inputs:

Test Plan:

Test # Input Values Expected Output Actual Output
#1 a: 3
b: 4
4
#2 a: 5
b: 2
5
#3 a: 6
b: 6
6
#4 a: 12
b: 4
12

Average Score?


The aim of this program is to calculate the average score out of 3 scores:

Test Plan:

Test # Input Values Expected Output Actual Output
#1 Score #1: 10
Score #2: 20
Score #3: 30
20
#2 Score #1: 0
Score #2: 0
Score #3: 30
10
#3 Score #1: 50
Score #2: 50
Score #3: 50
50
#4 Score #1: 20
Score #2: 70
Score #3: 30
40

Your age in the future?


The aim of this program is to calculate how old you will be in a given number of years:

Test Plan:

Test # Input Values Expected Output Actual Output
#1 Age: 15
Gap: 5
Age: 20
#2 Age: 18
Gap: 30
Age: 48
#3 Age: 20
Gap: 80
Age: 100

What’s my grade?


The aim of this program is for the user to input a test score and for the program to output a grade as follows:

  • Any score below 50 should receive a U grade
  • Any score between 50 and 69 should receive a Pass grade
  • Any score between 70 and 89 should receive a Merit grade
  • Any score of 90 or above should receive a Distinction grade


Test Plan:

Test # Input Values Expected Output Actual Output
#1 Score: 30 Grade: U
#2 Score: 65 Grade: Pass
#3 Score: 82 Grade: Merit
#4 Score: 97 Grade: Distinction
#5 Score: 102 Invalid Score!
#6 Score: 0 Grade: U
#7 Score: 50 Grade: Pass
#8 Score: 69 Grade: Pass
#9 Score: 70 Grade: Merit
#10 Score: 89 Grade: Merit
#11 Score: 90 Grade: Distinction
#12 Score: 100 Grade: Distinction

Tip of the day!


The aim of this program is to generate a tip of the day based on the weather conditions.

Test Plan:

Test # Input Values Expected Output Actual Output
#1 Sunny: Yes
Rainy: Yes
Snowy: No
Windy: Yes
You might see a rainbow today!
Wear a raincoat!
Have a good day!
#2 Sunny: No
Rainy: Yes
Snowy: No
Windy: No
Take your umbrella!
Have a good day!
#3 Sunny: No
Rainy: No
Snowy: Yes
Windy: Yes
Wear a raincoat!
Have a good day!

Factorial!


The aim of this program is to calculate the factorial of any given number. e.g. 5! = 5x4x3x2x1 = 120

Test Plan:

Test # Input Values Expected Output Actual Output
#1 5 5! = 120
#2 10 10! = 3628800
#3 0 0! = 1

HCF and LCM algorithms

In this Python challenge we will write two functions to calculate and return the Highest Common Factor (HCF) and the Lowest Common Multiple (LCM) of two numbers.

Highest Common Factor (HCF)


Note that the HCF is also known as the GCD (Greatest Common Divisor).

To calculate the HCF of two numbers we will write a function based on Euclid’s Division Algorithm. Your function called HCF() will take two parameters and will be based on the following flowchart:
Flowchart-HCF-Function

Python Code


Create the HCF() function on the Python trinket below:

Lowest Common Multiple


Once we have worked out the HCF of two numbers we can easily calculate their Lowest Common Multiple (LCM). The LCM of two numbers a and b is their product divided by their HCF:

LCM(a, b) = ab/HCF(a,b)

Your task is to create a new Python function called LCM() that takes two parameters a and b and returns the LCM of the two numbers using the above formula.

Test Plan


Use the following test plan to test both the HCF() and LCM() functions.

Test # Input Values Expected Output Actual Output
#1 a: 32
b: 24
HCF: 8
LCM: 96
#2 a: 45
b: 30
HCF: 15
LCM: 90
#3 a: 78
b: 24
HCF: 6
LCM: 312
#4 a: 60
b: 20
HCF: 20
LCM: 60
#5 a: 100
b: 21
HCF: 1
LCM: 2100
#6 a: 96
b: 72
HCF: 24
LCM: 288
unlock-access

Solution...

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

Euclid’s Division Algorithm

Euclid’s division algorithm is used to calculate the Highest Common Factor (HCF) of two positive numbers. It is based on Euclid’s division lemma and can be implemented in just a few lines of high level code. You can read more about this algorithm on this page.

Euclid’s Division Algorithm: Pseudocode

INPUT a #The largest of two numbers
INPUT b #The smallest of two numbers
WHILE b > 0
   temp = b
   b = a MOD b
   a = temp
END WHILE
OUTPUT a

Trace Table


Do get a better understanding of how this algorithm works we will complete the following trace tables assuming that the two input values a and b will be a = 32 and b = 24. The output (Highest Common Factor) of this program should be 8.

Complete the trace table below to find out if this algorithm will produce the required output.

 Line Numberabtempb > 0?OUTPUT
132
224
3True

Python Code


You can now create a function called calculateHCF() that takes two parameters, a and b and returns the HCF of these two numbers using Euclid’s division algorithm.

To improve your code, you should make it work even when number b is greater than number a.

Test Plan


Test your function using the following test plan:

Test # Input Values Expected Output Actual Output
#1 a: 32
b: 24
8
#2 a: 45
b: 30
15
#3 a: 78
b: 24
6
#4 a: 60
b: 20
20
#5 a: 100
b: 21
1
#6 a: 96
b: 72
24
#7 a: 72
b: 96
24

Tagged with:

Input, Output & Storage Devices: Quiz!

computer-hardwareComputers hardware consists of all the components that you will find inside the computer (Motherboard, CPU, RAM, graphic card, sound card, network card, etc.) as well as all the peripherals/devices than you can plug to a computer.

Peripherals are often categorised into three types:

  • Input Devices,
  • Output Devices,
  • Storage Devices.

  • input-output-devices-2

    Input Devices Output Devices Storage Devices
    Mouse
    Keyboard
    Microphone
    Webcam
    Scanner
    Barcode Reader
    Sensors
    Chip & Pin Card Reader
    Magnetic Stripe Card Reader
    Touchpad
    Game Controller
    Joystick
    Screen/Monitor
    Projector
    Speakers
    Headphones
    Printer (Inkjet or Laserjet)
    3D Printer
    Actuators
    LED
    Magnetic Hard drive
    SSD Hard drive
    USB Key
    CD Drive
    DVD Drive
    Bluray Disc/Drive
    SD Card Reader

    input-output-devices-1

    Take the Quiz! (open full screen)


ASCII Quiz Challenge

ASCII Code


The ASCII code (Pronounced ask-ee) is a code for representing English characters as numbers, with each character assigned a number from 0 to 127. For example, the ASCII code for uppercase A is 65. The extended ASCII contains 256 characters (using numbers from 0 to 255).

To see a list of the most useful ASCII codes, you can download our simplified ASCII helpsheet.

keyboard
Each character that appears on your keyboard has a unique value. This includes the 26 lowercase letters & 26 uppercase letters of the alphabet, the number digits from 0 to 9 and a range of punctuation signs and special characters.

Take the Quiz! (open full screen)


chr() and ord() functions

Using Python, you can easily access ASCII values of a character using the ord() function. And vice-versa, the chr() function is used to retrieve the character matching a given ASCII value. For instance:

  • ord(“A”) returns 65
  • chr(65) returns “A”

ASCII Quiz in Python

Your Python challenge consists of recreating the above game where the user tries to guess the ASCII value of different characters randomly selected. The difference between the user’s guess and the actual ASCII value of the character being displayed is used to decrement a life score.

The user starts the game with a life score of 255 and tries to guess as many ASCII values as possible before reaching a life score of 0. Any correct answer (exact ASCII value guessed) automatically resets the user’s life score to 255!

unlock-access

Solution...

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