More results...

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

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.

HTML Codes

When creating your webpages you may want to use sepcial characters. Using the the following codes in your HTML code:

Star Symbols

SymbolHTML CodeSymbolHTML Code
4 Tear-drop asterisk✢4 Balloon asterisk✣
Club asterisk✥4 Balloon asterisk heavy✤
4-pointed star black✦4-pointed star white✧
Stress outlined star✩Circled white star✪
Open center black star✫Black center white star✬
Black star outlined✭Black star outlined heavy✮
Pinwheel star✯White star shadowed✰
Asterisk heavy✱Open center asterisk✲
8-spoked asterisk✳8-pointed black star✴
8-pointed pinwheel star✵6-pointed black star✶
8-p. rectilinear✷8-p. rectilinear heavy✸
12-p. black star✹16-p. asterisk✺
Teardrop-spoked ast.✻Open center t-s. ast.✼
Heavy t-s. ast.✽6-p. b&w florette✾
Black florette✿White florette❀
8-p. outlined bl. florette❁8-p. circled o.c. star❂
Heavy t-s. pinwheel❃Snowflake❄
Tight 3foliate snowflake❅Heavy chevron snowfl.❆
Sparkle❇Heavy sparkle❈
Balloon asterisk❉8 t-s. propeller asterisk❊
Heavy 8 t-s. propeller ast.❋Bl. diamond w/ white X❖

HTML Notes and Music Symbols

SymbolHTML CodeSymbolHTML Code
quarter note♩eighth note♪
two eighths♫two sixteenths♬
flat note♭natural note♮
sharp note♯

Heart Symbols

SymbolHTML CodeSymbolHTML Code
Heavy black heart❤Heavy bl. heart rotated❥
Floral heart❦Floral heart rotated❧
Heart exclamation mark❢Heavy heart excl. mark❣
black heart♥red heart♡

Utility Symbols

SymbolHTML CodeSymbolHTML Code
telephone☎telephone outline☏
check box empty☐ballot box checked☑
ballot box with X☒Sign here✍
Check mark✓Check mark heavy✔
Multiplication X✕Multiplication X heavy✖
Ballot X✗Ballot X heavy✘
Bullet•Password dot●
Pencil diagonal down✎Pencil✏
Pencil diagonal up✐
Cut above✁Cut here✂
Cut below✃Scissors✄
Public pay phone✆Film reel✇
Airport/airplane✈Envelope/mail/email✉
Recycling♲Recycling black♻
@At sign@Fuel Pump⛽

Commercial Symbols

SymbolHTML CodeSymbolHTML Code
©copyright©Service Mark&#8480
®Registered trademark®Trademark™
¤Currency¤Euro€
¢Cent¢£Pound£
$Dollar&36;¥Yen¥
§Section§Number sign№

Hands, Fingers and Faces

SymbolHTML CodeSymbolHTML Code
Left-pointing☚Right-pointing☛
Left-pointing☜Upwards pointing☝
Right pointing☞Downwards pointing☟
Sign here✍victory sign✌
Frowning face☹Smiley face☺
Black smiley face☻

Science and Medical Symbols

SymbolHTML CodeSymbolHTML Code
Celsius℃Fahrenheit℉
OhmΩInverted Ohm℧
Skull & Crossbones☠Caution sign☡
Radioactive☢Biohazard☣
Caduceus☤Prescription℞

Weather and Celestial Symbols

SymbolHTML CodeSymbolHTML Code
Sunshine☀Cloudy☁
Rain☂Snow☃
Comet☄Star solid★
Star white☆Lightning☇
Thunder☈Sun☉
Waxing☽Waning☾
Sunshine☼

Religious and Spiritual Symbols

SymbolHTML CodeSymbolHTML Code
Ankh☥Eastern Christian Cross☦
Chi Rho Cross☧Patriarchal Cross☨
Greek Cross☩Greek Cross heavy✚
Greek Cross outline✙Saltire, St.Andrew’s Cross☓
Latin Roman Cross✝Latin Cross 3D shadow✞
Latin Cross outline✟Peace sign☮
Maltese Cross✠Star of David✡
Crescent Moon & Star☪Farsi symbol☫
Adi Shakti☬Hammer & Sickle☭
Yin & Yang☯Dharma Wheel☸
Trigram Heaven☰Trigram Lake☱
Trigram Fire☲Trigram Thunder☳
Trigram Wind☴Trigram Water☵
Trigram Mountain☶Trigram Heaven☷

HTML Zodiac Signs

SymbolHTML CodeSymbolHTML Code
Aries♈Taurus♉
Gemini♊Cancer♋
Leo♌Virgo♍
Libra♎Scorpio♏
Sagitarius♐Capricorn♑
Aquarius♒Pisces♓

HTML Planetary Symbols

SymbolHTML CodeSymbolHTML Code
Mercury☿Venus♀
Earth♁Mars♂
Jupiter♃Saturn♄
Uranus♅Neptune♆
Pluto♇

HTML Chess Symbols

SymbolHTML CodeSymbolHTML Code
White King♔White Queen♕
White Rook♖White Bishop♗
White Knight♘White Pawn♙
Black King♚Black Queen♛
Black Rook♜Black Bishop♝
Black Knight♞Black Pawn♟

HTML Card Symbols

SymbolHTML CodeSymbolHTML Code
black spade♠red spade♤
black heart♥red heart♡
black diamond♦red diamond♢
black club♣red club♧

My Weather Forecast

rainbow

Learning Objectives


In this challenge we are going to use selection blocks to display a weather forecast report.

Selection blocks uses if, elif and else statements to decide which block of code to run.

if statements use comparison operators to compare values and make a decision based on the comparison.

The main comparison operators are:

Comparison Operator Meaning Example
== is equal to 4==4
!= is different from 4!=7
< is lower than 3<5
> is greater than 9>2
<= is lower or equal to 3<=5
5<=5
>= is greater or equal to 9>=2
5>=5

When using comparison operators the result of a comparison is either true or false. For instance:

  • 3 < 5 returns true,
  • 3 > 5 returns false.

It is also possible to combine comparisons together using boolean operators such as and and or. This enables to check if more than one comparison are true (and operator) or whether at least one of the comparisons is true (or operator). e.g.

  • (3 < 5) and (3 < 7) returns true,
  • (3 == 5) or (3 < 9) returns true,
  • (3 == 5) and (3 < 9) returns false.

Check the code


Check the following code and notice how it is using:

  • Selection structures (using if statements),
  • Comparison operators (<, <=, >, >=, !=, ==)
  • Boolean logic (AND, OR) when using comparison operators within if statements

Task #1: Tweak the code


Your challenge consists of adding more to this weather report.
The same way this report randomly finds out if today will be sunny or rainy, add some code to find out whether it will be icy and whether it will be snowy.

If the weather is icy then display a message saying: “Be careful not to fall over on black ice.”

If the weather is either icy or snowy then display a message saying: “Put your gloves on, it’s going to be cold today.”

Task #2:


Add a criteria to find out whether it will be windy or not.

If the weather is both sunny and windy write a message saying: “It’s a good time to go sailing!”

If the weather is sunny but not windy write a message saying: “A perfect day to go to the beach!”

Task #3:


Add your own criteria and messages to complete this weather forecast report further.

Tagged with: ,

Year 2050

calendar-clockHave you got any idea how old you will be in 2050?

By completing the following challenges we will write computer algorithms for the computer to tell us more about our age in the future!

Getting Started


First you will need to copy the following code in your Python IDE or using an online Python editor.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

Challenge #1: How old will you be in…


ChallengeSolution
Complete the above code with a for loop to display the following messages:

  • In 2016 you will be … years old.
  • In 2017 you will be … years old.
  • In 2050 you will be … years old.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

for i in range(0,36):
  thisYear += 1
  age += 1
  print("In " + str(thisYear) + " you will be " + str(age) + " years old.")

Challenge #2: How old will you be in 2050 (without using a loop).


ChallengeSolution
Complete the above code without using a for loop to display the following message:

  • In 2050 you will be … years old.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

yearsDifference = 2050 - thisYear
age = age + yearsDifference
print("In 2050 you will be " + str(age) + " years old.")

Challenge #3: When were your born?


ChallengeSolution
Tweak your code so that the Python program calculates and displays the year you were born. For instance it will display:

  • You were born in ….

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

birthYear = thisYear - age
print("You were born in " + str(birthYear) + ".")

Challenge #4: In how many years will you turn 21!


ChallengeSolution
Tweak your code so that the Python program calculates and displays how many years till your turn 21. Note that if the end-user is older than 21, then the program should tell them how many years have passed since their 21st birthday.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

if age<21:
  yearsDifference = 21 - age
  print("You will be 21 in " + str(yearsDifference) + " years.")
elif age==21:
  print("You are 21 years old this year!")
elif age>21:
  yearsDifference = age - 21
  print("You were 21 years old " + str(yearsDifference) + " years ago.")

Extension


In all the challenges listed above we hardcoded the current year to 2015. This means that our programs will not work next year unless we change the code on line 1: from thisYear = 2015 to thisYear = 2016.

Instead we can improve our code by getting Python to find out what year we are currently in.

The following lines of code show you how to achieve this. You can reuse them to tweak your code.

unlock-access

Solution...

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

Resistor Value Calculator

Resistor Value Calculator

resistor-5When designing electronic circuits you may need to use resistors. Each resistor has a resistance value expressed in ohms (Ω). Resistors are identified by a series of concentric coloured bands. Each band colour has a value which is used to calculate the resistance value of the resistor.

Here is how the resistor colour code works:
Resistor-Color-Bands

Resistor-Color-Codes

You can check the resistance value of a resistor online using an online calculator.

Your Challenge


Your challenge consists of writing a program that asks the user to enter three colours (e.g. Red, Red, Green) and returns the resistance value (e.g. 2,200,000 Ω or 2,200 kΩ).

Test your code


Test your code to find the resistance value of the following three resistors:

Resistor1

Resistor2

Resistor3

Challenge #2: The other way round


Create a computer program that asks the user for a resistor value in ohms and returns the three colours matching this value.
unlock-access

Solution...

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

Meet E.V.E.

eveMeet E.V.E., a computer algorithm with artificial intelligence. E.V.E. can communicate with human beings. Well it’s only a start and we need you to complete the code provided below for E.V.E to be able to communicate more effectively with teenagers.

Learning Objectives


By completing this programming challenge you will learn how to code in Python. You will use the input() method to ask questions to the end user. You will store their answers using your own variables. You will use if statements to check the user answers and the print() method to write information on the screen.

Your Challenge

Complete the code below to let E.V.E. ask more questions to the end-user.


unlock-access

Solution...

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

Optical Illusions

illusion-1

Which of the above two purple circles is bigger?

Learning Objectives


In this challenge you will use Python Turtle to draw geometric shapes and optical illusions.

To help you we have created three functions as follows:

  • drawLine() to draw a straight lines between two points,
  • drawSquare() to draw a square of a given size and colour,
  • drawCircle() to draw a circle of a given size and colour.

To position these shapes on the screen you will need to use x,y coordinates and pass these coordinates when using the three functions given above.

(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:
xy-coordinates

Check the code

Your Challenge

Complete the code given to draw the following optical illusions:

illusion-1

Which of the above two purple circles is bigger?

illusion-2

Which of the above two lines is longer?

illusion-3

Which of the above two lines is longer?

illusion-4

Are both purple lines perfectly straight?

illusion-5

Are all the white dots really white?

illusion-6

Tagged with: ,