More results...

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

Top 10 Challenge


In this challenge, you will create a computer program to ask the end-user to try to guess the top 10 largest countries per area.

To do so you will use a list in Python, containing the top 10 countries as follows:

#Top 10 largest countries in the world per area
largestCountries = ["Russia","Canada","USA","China","Brazil","Australia","India","Argentina","Kazakhstan","Algeria"]

Using this list, what do you think would be the values of:

  • largestCountries[0],
  • largestCountries[2],
  • largestCountries[9]?

We have started the code by asking the user to enter the name of a country and checking whether or not this country is in the top ten list.

Your aim is to tweak this code to allow the user to name as many countries as they wish. Each time they get one country from the top 10 list, they score one point. The end-user should not need to list the ten countries in the right order. However the user should not score additional points by naming the same country more than once.

The program should stop if the user has guessed all 10 countries or when the user presses “x” to quit the game.

Extension Task


Use the time library and use a timer to give a maximum of 60 seconds for the user to guess all ten countries.
unlock-access

Solution...

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

Planets Top Trumps

For this challenge we will create a variant of the game of Top Trumps using 9 cards, one for each of planet of our Solar System.

We will store all the cards and data about the planets in a list of lists, equivalent to a two-dimensional array, called planets. The “array” will contain 9 rows (from 0 to 8) and 5 columns (0 to 4) with the following columns/fields:

  • Name of the planet,
  • Distance from the Sun (in million km),
  • Size (Diameter) in km,
  • Orbital Period in days,
  • Number of Moons.

We will then be able to access a single piece of data using indexes as follows:
planets-list-of-list

Game Rules


We will write a python program based on the planets array (list of lists) which:

  1. Randomly picks a planet/card from the deck and display all its data on screen,
  2. Randomly picks a planet for the computer, without displaying it on the screen,
  3. Asks the end-user which criteria to compare for this round: (1-Distance from the Sun, 2-Size, 3-Orbital Period,4-Number of Moons),
  4. Compares both cards using the chosen criteria,
  5. Tells the player whether they won, drew or lost,
  6. Gives the computer or the player points: 3pts for a win, 1pt for a draw,
  7. Displays both the computer and the player’s scores and repeats the whole process until the player or the computer reaches a score of 12 and hence win the game.

Complete The Code


Tagged with:

US Presidents Quiz

american-presidentSince the establishment of the United States of America, in 1789, 45 people have served as president, the first one being George Washington.

For this challenge you will use a text file listing all of these US presidents in chronological order. (See text file below.) You will then write a Python program to randomly pick two presidents from this list of 45 presidents. Your program will display the name of both presidents and ask the end-user who is the most recent president amongst the two presidents being displayed. For instance the program will output the following question:

Who was the most recent president?
A) John F. Kennedy or B) Richard Nixon.

The game carries on as long as the user is correct. For each correct answer the user will score 10 points. The game ends once the user gets a wrong answer.

To complete this challenge you will need to read more about how to read through a CSV file.

The text file we will use contains one line per US President. On each line it stores a number (position from 1 to 45), the name of te president, the year their presidency started, the year it ended as follows:

Number,Name,StartYear,EndYear
TextFileus-presidents.csv

Complete the Code


Extension


When asking the question, give the user the option to ask for a clue (by typing answer C). When the user asks for a clue, the program will output the starting and ending date of the presidency of one of the two presidents. The user will then have to answer the intial question.
unlock-access

Solution...

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

Break Even Point

In Business or Economics the Break Even Point (BEP) is the point at which the total of fixed and variable costs of a business becomes equal to its total revenue. At this point, a business neither earns any profit nor suffers any loss. The following graph explains all the concepts used to find out the break even point:
break-even-point

Scenario

Let’s consider an online Bakery that specialises in selling Birthday Cakes online. The owner of the bakery has estimated all the costs involved and grouped them into two categories:

  • Fixed Costs: These costs will remain the same and do not vary depending on the number of cakes being sold. They include:
    • The rent,
    • Purchasing Cooking Equipment,
    • Utility Bills,
    • Website (Setup and maintenance/hosting).
  • Variable Costs: These costs will vary depending on the number of cakes being sold. They include:
    • Ingredients,
    • Packaging,
    • Delivery Fees.

The total costs of this online bakery consist of the fixed costs plus the variable costs and can be calculated using the following formula:

Total Costs = fixed costs + (variable costs x number of cakes sold)

The revenues of this online bakery consist of the money received when selling cakes. They can be calculated using the following formula:

Revenues = average cake price x number of cakes sold

The question we are trying to answer is as follows:

What is the break even point of this online bakery? In other words, how many cakes will need to be sold to cover all the costs and start making a profit?

We can answer this question using the following formula:

Number of Cakes to break even = Fixed Costs / (Average Price – Variable Costs)

Coding Challenge

We have listed all the costs of our online bakery, both fixed and variable into a text file.

The average price per cake is £16.00.

Use a Python program to extract the relevant information from the text file in order to:

  • Calculate the sum of all fixed costs.
  • Calculate the sum of all variable costs.
  • Find out the break even point for this business: how many cakes will need to be sold to cover all the costs and start making a profit?

The text file we will use contains one line per cost as follows:

Description;Type of cost (fixed cost or variable cost);Value (in pounds);
TextFilecosts.txt

To complete this challenge you will need to read more about how to read through a CSV file.


unlock-access

Solution...

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

Curling Challenge

curling-targetCurling is a sport in which players slide stones on a sheet of ice towards a target area which is segmented into four concentric circles. Two teams, each with four players, take turns sliding heavy, polished granite stones, also called rocks, across the ice curling sheet towards the house, a circular target marked on the ice. Each team has eight stones. The purpose is to accumulate the highest score for a game; points are scored for the stones resting closest to the centre of the house at the conclusion of each end, which is completed when both teams have thrown all of their stones. A game usually consists of eight or ten ends.

Challenge #1


We have created a Python script using Python Turtle to represent the target as well as the eight stones, randomly positioned above the target. Our algorithm randomly generates x and y coordinates between -170 and 170 for both coordinates. Each stone is a circle with a radius of 24 pixels. However our algorithm is not realistic as sometimes the stones are overlapping which would be impossible in a real game of curling.

curling-overlapping

Your challenge consists of tweaking this code to prevent two stones from overlapping. To ensure that there is no overlap between two stones you will need to use Pythagora’s theorem:

curling-calculations

Complete the code


Challenge #2


The team who wins is the team who has the closest stone (to the centre of the target: coordinates (0,0)).

In this case the team scores points based on the number of stones resting closer to the target than any of the stones of the opposite team.

Your challenge consists of tweaking your code to find out who is the winning team and to calculate the score of the winning team.

curling-scoring-system

unlock-access

Solution...

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

Darts Scoring Algorithm

The following diagram explains how a dart is allocated a score in a game of darts.
dartboard-scores

To calculate the score of an arrow based on its (x,y) coordinates we will use two calculations:

  • The distance of the arrow from the centre of the target,
  • The angle matching the arrow position.

Calculating the distance


To calculate the distance of the arrow from the centre of the target, we will use the Pythagora’s Theorem:
dartboard-pythagoras
You can review this blog post where we have used a similar approach to calculate the score of an arrow in a game of archery.

We will then be able to use this distance to check where the arrow landed:

  • Bull’s Eyes (Red inner circle)
  • Bull (Green inner circle)
  • On target (Black or white zones)
  • On the triple ring
  • On the double ring
  • Outside of the target

To do so you will need the dimensions of the target (in pixels). These are given on the diagram below:
dartboard-distances

Calculating the angle


To calculate the angle we will use the trigonometric formulas (SOCATOA):
dartboard-trigonometric-formula

Using the following angles your program can then figure out the score for the arrow:
dartboard-angles

Complete the code


Check the code below used to display the dartboard and to throw an arrow by generating random (x,y) coordinates.

Complete the code to calculate both the distance and the angle of the arrow based on the (x,y) coordinates of the arrow. Use this information to find out the actual score of the arrow.

Extension Task


Adapt your code so that the computer throws three darts. Your script should then calculate the total score of the three darts.
unlock-access

Solution...

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

Factorial Challenge

In Mathematics, the factorial of n is denoted by n! and calculated by the product of integer numbers from 1 to n.

For instance:
factorial-of-5

In this challenge you will write a Python program that asks the user to enter a positive number. In return the program will output the factorial of this number.

Iterative Approach


n! = n x (n-1) x (n-2) x (n-3) x … x 4 x 3 x 2 x 1

Your task: Write a function to calculate n! using an iterative approach.

Recursive Approach


Another approach to define the factorial of a number n is to use a recursive approach:

n! = n x (n-1)!

Your task: Write a function to calculate n! using a recursive approach.

0! = 1


The factorial calculations given above work for any positive integer greater than 0.

There is however an exception for the value 0 as 0! = 1.

Amend your code to ensure that if the user enters the value 0, both your functions return the value 1.

Also, add some input validation to ensure that the user does not enter a negative value.

Help?


Check this blog post for some help on how to complete this challenge.

Did You Know?

Six weeks is exactly 10! seconds (=3,628,800)
6 Weeks, 7 Days per week: 6 x 7 days
6 Weeks, 24 hours per day: 6 x 7 x 24 hours
6 Weeks, 60 minutes per hour: 6 x 7 x 24 x 60 minutes
6 Weeks, 60 seconds per minute: 6 x 7 x 24 x 60 x 60 seconds
Factor some numbers: 6 x 7 x (8 x 3) x (3 x 4 x 5) x (2 x 3 x 10)
Rearrange: 10 x 3 x 3 x 8 x 7 x 6 x 5 x 4 x 3 x 2
Lastly as 3 x 3 = 9 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2

playingcards

There are 52! ways to shuffle a deck of cards.

That is 8.0658175… × 1067

unlock-access

Solution...

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

Kings & Queens of England

Portrait of Henry VIII
Portrait of Henry VIII

There have been 66 monarchs of England and Britain spread over a period of 1500 years.

For this Python challenge you will use a text file listing all of these monarchs in chronological order. You will then write a Python program to prompt the user to enter a year (between 757 and 2017) and your program will look up for the name of the monarch of England at the given date.

To complete this challenge you will need to read more about how to read through a CSV file.

The text file we will use contains one line per monarch. On each line it stores the year the reign started, the year it ended and the name of the monarch as follows:

StartYear,EndYear,Name
TextFileMonarchs-of-England.csv

Complete the Code


Testing


Once you have completed the code check that it produces the expected output by performing the following tests:

Test # Input Values Expected Output Actual Output
#1 1192 Richard I
#2 1345 Edward III
#3 1042 Harthacanut
Edward the Confessor
#4 1547 Henry VIII
Edward VI
#5 1016 Ethelred II the Unready
Edmund lronside
Cnut (Canute)
#6 512 Not Found

Extension Task:


Tweak the output of your program so that when giving the name of the monarch matching the year entered, your program also mentions who were the previous monarch and the next monarch.

For instance the output of the program could be:

In 1192, the monarch of England was Richard I. Their predecessor was Henry II up until 1189 and their successor was John whose reign started in 1199.

Video Tutorial



unlock-access

Solution...

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

Professor Snape’s Magic Potions

invisibility-potionIn this challenge we will use file handling techniques to read through and access the content of different text files.

We have saved three recipes for the following three magic potions using one text file per recipe:

  • Invisibility Potion:
    A potion that makes you invisible for a short period of time.
  • Aging Potion:
    A potion which causes the drinker to temporarily become older. The more of the potion is drunk, the larger the aging which occurs.
  • Poison Antidote:
    A potion which counteracts ordinary poisons, such as creature bites and stings.

clover
The following Python code opens the text file of the invisibility potion (invisibility-potion.txt) in read-only mode. It then uses a for loop to access and print the content of each line, one line at a time.

Your Challenge


Your challenge consists of adding a menu to this code to let the user decide which potion to display on screen. Based on their choice, the program will open the relevant text file and display the recipe on screen.

Extension Task


Add an extra text file to store this new potion:

Babbling Beverage
A potion that causes uncontrollable speaking of nonsense.

    Add 1 four-leaf clover and 3 dragon scales into a mortar
    Add 1 garlic clove and crush the ingredients
    Pour the ingredients into a copper cauldron
    Add 2 horned slugs in the cauldron
    Add 3 drops of beetroot juice
    Boil and simmer the potion for half an hour
    Stir once every 10 minutes, clockwise

Video Tutorial



unlock-access

Solution...

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

Mind the Gap

mind-the-gapWhen your browser displays a webpage on screen it uses 3 different programming languages called HTML, CSS and JavaScript. We call the combination of these three languages “client-side web technologies”.

Each of the three language has its own syntax and purpose:

  • HTML uses <tags> and its purpose is to add content on the page such as text, pictures, and video clips.
  • CSS is used to customise the look and feel of the page by defining the position of each element on the page, defining the layout of the page as well as formatting text, pictures and other components on the page.
  • JavaScript is used to add user interaction to the page. JavaScript can be linked to HTML tags through various events such as the onClick event of a button.

Each of these languages are based on web standards defined by the World Wide Web Consortium W3C. As a consequence these languages are recognised by all the main web-browsers including Google Chrome, Microsoft Edge, Modzilla Firefox, Safari, etc. on various platforms (Windows, MacOs, Linux, Android etc.)

The Following codepen provides a clear demonstration of the syntax and purpose of HTML, CSS and JavaScript and shows how JavaScript can interact with various HTML tags.

On this codepen you can click the button located below the “Mind the Gap” sign in order to change the text of the sign.

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

Your Challenge:


Add one more button to allow the user to change the background colour of the both sections of the logo (circle and label).

To do so you will need to:

  • Add a button below the poster
  • Create a new Javascript function called changeColours() and add Javascript code to prompt the user to enter two colour codes (hexadecimal format: e.g. #00FF00). Use some of the code as provided below to change the colour of the red circle or of the blue label.
    document.getElementById("red-circle").style.borderColor = "#FFFF00";
    document.getElementById("blue-label").style.backgroundColor = "#00FF00";

You can then use your web application to re-create the following signs:underground-hello-world
underground-sign

Tagged with: , ,