More results...

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

HTML Code Builder (in Python)

html-tag

Learning Objectives


By completing this challenge you will learn how to use count-controlled loops (for loops). You will include a loop within a loop: This is called nesting.

In this challenge you will also use string concatenation techniques.

Finally you will also learn about the following tags in HTML:

  • <UL>, <LI> tags used to create bullet point lists in HTML,
  • <TABLE>, <TR> and <TD> tags used to create tables in HTML.

Bullet Point Lists in HTML


In HTML you can create bullet point lists to display information on the page as follows:
  • First bullet point,
  • Second bullet point,
  • and so on…

To do so you need to use both a <UL> tag to open and close the list and a <LI> tag for each bullet point within the list.
Here is HTML the code:

<UL>
	<LI>First bullet point,</LI>
	<LI>Second bullet point,</LI>
	<LI>and so on...</LI>
</UL>

Using Python we have written a script that prompts the user to enter the number of bullet points they need. In return the script produces the HTML code for the user to copy and paste to their webpage.

HTML Tables


From time to time, when building a webpage in HTML you need to present your data using a table.

A table (<TABLE>) is made of rows (<TR>). Each row is made of data cells (<TD>).

So for instance a 3×2 table contains 3 rows and each row contains 2 data cells. The HTML code of such a table is as follows:

HTML-Table

This is the full code in HTML:

<TABLE>
	<TR>
		<TD> ... </TD>
		<TD> ... </TD>
	</TR>
	<TR>
		<TD> ... </TD>
		<TD> ... </TD>
	</TR>
	<TR>
		<TD> ... </TD>
		<TD> ... </TD>
	</TR>
</TABLE>

Check this other example of table from w3schools.

Your Challenge


Write a python script (or reuse the script above) to prompt the user to enter the number of rows and the number of columns they need for their table. The program should then generate the HTML code for the required table.

Extension Task:


Check how this online HTML Table Code Generator works.

Update your code to ask for additional settings such as:

  • Table width,
  • Table alignment,
  • Cell padding,
  • Background Colour,
  • Border thickness and colour,
  • etc…

Your program should then generate the HTML code for the table, including the given parameters using HTML or CSS attributes.

unlock-access

Solution...

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

Calculating Pi using a Python script

Did you know?


Pi (π) is one of the most important and fascinating numbers in mathematics. Roughly 3.14, it is a constant that is used to calculate the circumference of a circle from that circle’s radius or diameter. Pi is also an irrational number, which means that it can be calculated to an infinite number of decimal places without ever slipping into a repeating pattern. This makes it difficult, but not impossible, to calculate precisely.

How to calculate Pi?


Method #1


All we need to calculate Pi is a round object such as a golf ball and a tape measurer.
We will need to take two measurements as follows:

  • Diameter,
  • Circumference

Knowing that Circumference = π x Diameter we can calculate π as follows: π = Circumference / Diameter.
pi-calculation-method-1
As you may have noticed, this method does not give you the exact value of Pi. This due to the fact that the measurements of the diameter and of the circumference of an object are never 100% accurate.

Challenge #1


Your challenge consists of writing a Python script that prompts the end-user to enter both the diameter and the circumference of a round object. Your program should return the corresponding estimation of π by using the formula from method #1: π = Circumference / Diameter.

You can try your algorithm with the following measurements:
pi-calculation-method-1-earth

Method #2: Calculating Pi Using an Infinite Series (Gregory-Leibniz series)


Mathematicians have found several different mathematical series that, if carried out infinitely, will accurately calculate Pi to a great number of decimal places. Some of these are so complex they require supercomputers to process them. One of the simplest, however, is the Gregory-Leibniz series. Though not very efficient, it will get closer and closer to Pi with every iteration, accurately producing Pi to five decimal places with 500,000 iterations. Here is the formula to apply:
pi-calculation-method-2

Challenge #2


Write a Python script that will calculate Pi with at least three accurate decimal places using the Gregory-Leibniz series.
Tip: You will need to use a for loop and decide of a number of iteration for your algorithm.

Method #3: Calculating Pi Using an Infinite Series (Nilakantha series)


The Nilakantha series is another infinite series to calculate Pi that is fairly easy to understand. While somewhat more complicated, it converges on Pi much quicker than the Gregory-Leibniz formula. Here is the formula to apply:
pi-calculation-method-3

Challenge #3


Write a Python script that will calculate Pi with at least three accurate decimal places using the Nilakantha series.

Help?


Our flowcharts can help you complete these three challenges.
unlock-access

Solution...

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

Word Score Challenge

word-score

Did you know?


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 M is 77. 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.

Using Python you can easily access ASCII values of a character using the ord() function. For instance ord(“M”) returns 77 and chr(77) returns “M”

Your Challenge

Write a Pyhon script that prompts the end-user to enter a word, e.g. “Hello”
The script should calculate the score of this word using the letter values (A=1, B=2, C=3…)

So if the use inputs the word “Hello” the script should output: 8 + 5 + 12 + 12 + 15 = 52%.

Solution


Hide SolutionView SolutionExtended Solution
This solution uses a for loop to access each letter of the word one letter at a time.
Using the ord() function it returns the ASCII code for the letter.
The ASCII code for A is 65, for B is 66 and so on. So by taking away 64 to the ASCII code we then get A=1, B=2, C=3 etc.

word = input("Type a word").upper()
wordScore=0

for letter in word:
  letterValue=ord(letter) - 64
  wordScore += letterValue
  
print(str(wordScore) + "%")
This solution is exactly the same as the previous one but displays the full formula to calculate the score e.g. “8 + 5 + 12 + 12 + 15 = 52%”. This formula is stored as a string in a variable called addition.

word = input("Type a word").upper()
wordScore=0
addition=""

for letter in word:
  letterValue=ord(letter) - 64
  if (addition==""):
    addition = str(letterValue)
  else:
    addition = addition + " + " + str(letterValue)
  wordScore += letterValue
  
print(addition + " = " + str(wordScore) + "%")
unlock-access

Solution...

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

Name the colour

paint-bucketsComputer programmers, web-designers, graphic designers often have to choose specific colours to design eye-catchy user interfaces. To do so they use colour codes which are used to uniquely identify each of the 16 million colours available on the RGB colour palette.

Some of the colours from this palette however have names such as red, white, blue, cyan, magenta, brown, yellow, etc.

For this challenge we are going to write a program that asks the user to type the name of colour and returns its matching colour code.

Let’s look at the code

Let’s test this code

Your task is to complete the following test plan to see if your program works as expected:

Test # Purpose Input Value Expected Output Actual Output
#1 Test to see if the program finds a colour that exists. Cyan #00FFFF
#2 Test to see if the program finds a colour that exists. Purple #8E35EF
#3 Test to see if the program displays a meaningful message if it cannot find the colour. rainbow “We cannot find your colour”
#4 Test to see if the program displays a meaningful message if the user did not enter any colour name (leave blank) “Make sure you type the name of a colour.”
#5 Test to see if the program still works without being affected by the case of the input value. pUrPLE #8E35EF

Fixing Bugs


While testing you may have noticed that test #4 did not produce the expected output.

Your task is to tweak the code to make sure it validates the user input. If the user did not type anything when asked to enter the name of the colour you will display a message saying: “Make sure you type the name of a colour.”

Final Testing


Now that you have fixed this issue, you are going to test it to see if you get the expected outcome.

Test # Purpose Input Value Expected Output Actual Output
#4 Test to see if the program displays a meaningful message if the user did not enter any colour name (leave blank) “Make sure you type the name of a colour.”

Extension Task:


Can you tweak this code so that if the user types a colour code (beginning with a “#” tag) the program finds the name of this colour.

Make sure you fully test your program!

Tagged with: ,

My thesaurus

thesaurusYour English teacher has asked you to write a description of your favourite imaginary pet. They want you to use plenty of adjectives in your description and suggest that you use a thesaurus to find synonyms to use in your description.

synonym

As a computer scientist, you have decided to write a computer program that will take a description of a pet as an input, lookup for synonyms in a thesaurus and output an alternative description of the pet.

Let’s look at the code

Let’s test this code

Copy the following description and use them with the code provided above. Copy the improved description in the output column.

Pet Input Output
pet-1
pets-2
pets-3
pet-4
Tagged with: ,

HTML Chess Challenge

chessboard2

Learning Objectives


In this challenge you are going to learn how to create and format a table using HTML. You will also learn how to insert special characters (using UNICODE characters) on to a webpage.
First you may want to learn about the three HTML tags used to create a table: <TABLE>,<TR> and <TD>

  • Tables are defined with the <TABLE> tag,
  • Tables are divided into table rows with the <TR> tag,
  • Table rows are divided into table cells with the <TD> tag.

Find out more about HTML tables…

HTML Chess Symbols

SymbolHTML CodeSymbolHTML Code
White King&#9812;White Queen&#9813;
White Rook&#9814;White Bishop&#9815;
White Knight&#9816;White Pawn&#9817;
Black King&#9818;Black Queen&#9819;
Black Rook&#9820;Black Bishop&#9821;
Black Knight&#9822;Black Pawn&#9823;

Challenge


Complete the code below to display a full 8×8 chessboard. (Click “Edit with CodePen” to be able to edit the code)

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

unlock-access

Solution...

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

Where are you calling from?

phone
Have you ever received a phone call from an unrecognised number and wondered who was calling and where is the person calling you located? Did you know that, in the UK, for landline phone numbers (not mobile phones), the first 5 digits of the number are called the area code and gives you in indication of where this phone number originates from.

For instance any phone number starting with 01624 comes from the Isle of Man, whereas a number starting with 01224 comes from Aberdeen.

The aim of this challenge is to write a program that lets the user input a UK based landline phone number. The program will then perform a linear search on a large CSV file containing all of the UK based area codes and their matching locations. This linear search will enable the program to identify the exact location of the given phone number.

Learning Objectives


By completing this challenge you will further improve your file manipulation techniques.

If you cannot remember how to open and read through a text file or CSV check our blog post about “how to access text files using Python”.

Your Challenge


Your challenge consists of writing a program that will:

    Prompt the end-user to enter their telephone number.
    Extract the area code (e.g. first 5 digits) for this number
    Perform a linear search for this area code using the CSV provided file.
    If the location is found, display the location this phone number is linked to.
    If the location is not found, display a message to let the user know the location cannot be identified.

Here is the CSV file our programs will be based on:


TextFile
phonecodes.csv

Complete the Code


Video Tutorial


Test Plan


Once your code is done, complete the following tests to check that your code is working as it should:

Test # Input Values Expected Output Pass/Fail?
#1 01424000000 Hastings
#2 01223999999 Cambridge
#3 01736123456 Penzance
#4 01401000000 Not found!

Extension #1


If the area code cannot be found in the CSV file it may be because some locations, such as Manchester, only use the first 4 digits (e.g. 0161) as their area code. Adapt your code, so that if an area code cannot be found, the program performs a second linear search, but this time only looking for the first 4 digits of the phone number.

Extension #2

The CSV file also gives you the longitude and latitude coordinates of each area code. These coordinates can be used to calculate the distance between two sets of coordinates on the earth, using the Haversine formula. You could adapt your script to calculate the distance in miles between two UK phone numbers.

To find out more about the Haversine formula you may want to complete this challenge first: UK Postcodes distance calculator.

unlock-access

Solution...

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

String Manipulation – Lesson Code

timetable

Learning Objectives


In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:

  • LEFT: to extract characters at the beginning of a string,

  • RIGHT: to extract characters at the end of a string,

  • LENGTH: to find out the number of characters in a string,

  • MID: to extract a set number of characters at a given position in a string,

  • LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.

LEFTRIGHTLENGTHMIDLOCATE
e.g. LEFT(myString, 5)

>>> to extract the first 5 characters of a string.

e.g. RIGHT(myString, 5)

>>> to extract the last 5 characters of a string.

e.g. LENGTH(myString)

>>> to retrieve the number of characters in a string.

e.g. MID(myString, 3, 5)

>>> to extract a set number of characters at a given position in a string.

e.g. LOCATE(“World”, “Hello World”)

>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.

Your Challenge


A timetabling system used by school stores information about each lesson such as the day and period of the lesson, the subject, the class teacher, and the room number. The system enables us to extract all these details in the following format:

Year Group-Subject-Day of the Week-Week Number (1 or 2):Period of the day (1 to 5)-Title (Mr/Mrs)-Teacher Name-Classroom

For examples we have extracted these 4 lessons from the system:

8-ICT-Mo1:3-Mrs.Johnson-A12
9-PE-Tu2:4-Mr.Smith-B8
11-Art-We2:1-Mr.Taylor-C12
13-Hi-Th1:3-Mrs.Fox-A9

Check the code below which is based on the 5 string manipulation functions we mentioned above: Left, Right, Length, Mid, and Locate.

Your challenge is to tweak this code to extract all the information from a lesson code and display it in the following format:

    Lesson Code: 8-ICT-Mo1:3-Mrs.Johnson-A12
    Year Group: 8
    Subject: ICT
    Week Day: Monday
    Week Number: 1
    Period: 3
    Teacher: Mrs Johnson
    Classroom: A12

Tagged with: ,

String Manipulation & ASCII Art

asciiart

Learning Objectives


In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:

  • LEFT: to extract characters at the beginning of a string,

  • RIGHT: to extract characters at the end of a string,

  • LENGTH: to find out the number of characters in a string,

  • MID: to extract a set number of characters at a given position in a string,

  • LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.

LEFTRIGHTLENGTHMIDLOCATE
e.g. LEFT(myString, 5)

>>> to extract the first 5 characters of a string.

e.g. RIGHT(myString, 5)

>>> to extract the last 5 characters of a string.

e.g. LENGTH(myString)

>>> to retrieve the number of characters in a string.

e.g. MID(myString, 3, 5)

>>> to extract a set number of characters at a given position in a string.

e.g. LOCATE(“World”, “Hello World”)

>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.

Your Challenge


Look at the pixel art below. The user enters their name and the computer generates an ASCII art. However the name of the user does not always fit within this pixel art.

Your challenge is to change this code to make sure that the Ascii Art takes into consideration the number of characters in the user’s name to make sure the Ascii art always looks fine.


unlock-access

Solution...

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

String Manipulation

typewriter

Learning Objectives


In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:

  • LEFT: to extract characters at the beginning of a string,

  • RIGHT: to extract characters at the end of a string,

  • LENGTH: to find out the number of characters in a string,

  • MID: to extract a set number of characters at a given position in a string,

  • LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.

LEFTRIGHTLENGTHMIDLOCATE
e.g. LEFT(myString, 5)

>>> to extract the first 5 characters of a string.

e.g. RIGHT(myString, 5)

>>> to extract the last 5 characters of a string.

e.g. LENGTH(myString)

>>> to retrieve the number of characters in a string.

e.g. MID(myString, 3, 5)

>>> to extract a set number of characters at a given position in a string.

e.g. LOCATE(“World”, “Hello World”)

>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.

Your Challenge


Check the code below which is based on the 5 string manipulation functions we mentioned above: Left, Right, Length, Mid, and Locate.

Your challenge is to tweak this code to ask the user to enter their school username in the following format:

  • Year Group Using two digits (e.g. “07” for year 7, “11” for year 11, “00” for staff members)
  • 1 character for their initial (first letter of their firstname)
  • The user’s lastname
  • A 2-digit code: “_S” for students, “_T” for teachers, “_A” for admin staff.

For instance the following usernames are valid usernames:


07jFox_S
09kJohnson_S
11rTaylor_S
00pJones_T
00jOliver_A

Your program should read the username and decide:

  • If the username is less than 6 characters long the program should ask the user to enter a valid username.
  • If the username does not contain the character “_” it should also ask the user to enter a valid username.
  • If the username is valid, the program should decide if the user is a member of staff or a student.
  • If they are a student the programme should find out their year group.
  • The program should also display the initial of the student as well as their lastame.
  • Finally the program should display whether the user is a Student, a Teacher or an Admin member of staff.