More results...

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

How Old is Your Cat?

catIn this challenge we are going to create a cat’s age convertor find out how old a cat is in “human years”. This is very useful to understand more about cats and the care they need and to find out at what stage of life a cat is.
To convert the age of a cat in human years you have to do the following:

  • Year 1 counts for 15 Human Years
  • Year 2 counts for 9 Human Years
  • Thereafter each year counts for 4 Human Years

For example:

  • A 1 year old cat is 15 Human Years old,
  • A 2 years old cat is 15 + 9 = 24 Human Years old,
  • A 3 years old cat is 15 + 9 + 4 = 28 Human Years old,
  • A 4 years old cat is 15 + 9 + 4 + 4 = 32 Human Years old,
  • A 5 years old cat is 15 + 9 + 4 + 4 + 4= 36 Human Years old,
  • A 10 years old cat is 15 + 9 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4 = 56 Human Years old.

You can check the full cat’s age conversion chart at the bottom of this post.

Your Challenge

Complete the following code to ask the user to input the age of a cat in years and return the equivalent in human years.

Extension #1

Amend your code so that it also works for cats under 1 year old. In this case you will have to ask for the cat’s age in months and use the conversion chart below (At the bottom of this post) to return the cat’s age in human years.

Extension #2

The stages of life of a cat are as follows:

  • Kitten – from birth to 6 months,
  • Junior – from 7 months to 2 years,
  • Prime – from 3 years to 6 years,
  • Mature – from 7 years to 10 years,
  • Senior – from 11 years to 14 years,
  • Geriatic – from 15 years above.

Extend your code to tell the end-user what stage of life their cat’s age is corresponding to.

Extension #3

Create another program where the user enters their age in human years and the program tells the user how old they would be if they were a cat!

Cat’s age conversion chart:

Life Stage Cat years Human years
Kitten2 months9 to 10 months
3 months2 to 3 years
4 months5 to 6 years
5 months8 to 9 years
6 months10 years
Junior8 months13 years
1 year15 years
2 years24 years
Prime3 years28 years
4 years32 years
5 years36 years
6 years40 years
Mature7 years44 years
8 years48 years
9 years52 years
10 years56 years
Senior11 years60 years
12 years64 years
13 years68 years
14 years72 years
Geriatric15 years76 years
16 years80 years
17 years84 years
18 years88 years
19 years92 years
20 years96 years
21 years and above100 years
unlock-access

Solution...

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

How many sweets in the jar?

how-many-sweets-in-the-jar“Guess how many sweets are in the jar” is a fundraising game. A large see-through jar is filled with a carefully counted number of sweets. People then try to guess how many sweets are in the jar. Each contestant has to donate a small amount of money to have a go. Guesses are recorded (name + guess). At the end of the game, the nearest guesser wins the content of the jar.

In this challenge we are using a Python script to record the names and guesses (number of sweets) of each contestant. We record this information in a list of lists called guesses.

Your challenge is to complete this code from line 63 to scan through all the guesses from this list and find out who has the nearest guess!

unlock-access

Solution...

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

Pascal Triangle

Pascal-triangle-animationIn mathematics, one of the most interesting number patterns is Pascal’s Triangle. It is named after Blaise Pascal (1623 – 1662), a famous French Mathematician and Philosopher.

To build a Pascal Triangle we start with a “1” at the top. We then place numbers below each number in a triangular pattern: Each number is the result of adding the two numbers directly above it. (See animation)

For this challenge we will use a Python script to output a Pascal Triangle after a fixed number of iterations (rows): e.g.

Pascal-Triangle

To do so we will use the following programming concepts:

  • Iteration: Each iteration will output one line of the triangle
  • List: We will use a list to store all the values appearing on one row
  • String Manipulation: We will use string manipulation techniques to format the output printed on screen (e.g. Our Pascal Triangle should appear centered on screen)

Python Code


We have started the code for you. You will need to complete this code form line 20.

unlock-access

Solution...

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

Infinite Quarter Series

The infinite quarter series is a series where each term is a quarter of the previous one:
infinite-quarter-series

We can visually represent this series by dividing a canvas (or price of paper) into 4 quadrants and colouring in one quadrant (bottom left). Then we repeat this process by dividing the top right quadrant into 4 and so on.

infinite-quarter-series-visual-representation-square

By doing so infinitely we will colour in a third of the initial canvas. This is because this infinite series converges to the value of 1/3.

Visual Demonstration using Python Turtle


Your Challenge


It is possible to visually represent this series using a triangle instead of a square.
Your task is to adapt the above Python script to represent this series using a triangle.
infinite-quarter-series-using-triangles

Help?


Check our flowchart to solve this challenge.
unlock-access

Solution...

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

Estimating Pi using Buffon’s Needle

Painting by François-Hubert Drouais - Musée Buffon à Montbard, France

Painting by François-Hubert Drouais – Musée Buffon à Montbard, France

A few hundred years ago people enjoyed betting on coins tossed on to a wooden floor … would they cross a line or not?

A French mathematician called Georges-Louis Leclerc, Comte de Buffon (1707-1788) started thinking about this and worked out the probability.

This probability is called “Buffon’s Needle” in his honor.

Buffon then used the results from his experiment with a needle to estimate the value of π (Pi). He worked out this formula:

π ≈ 2LN / CW

Where

  • L is the length of the needle
  • N is the total number of needles
  • C is the total number of needles crossing a line
  • W is the line spacing (Width of the wooden boards on the floor)

We have decided to simulate this experiment using a Python script (using Python Turtle).

Task 1


Run the above script and count how many needles (out of 50) are crossing a line. Apply Buffon’s formula to estimate the value of Pi using:
π ≈ 2LN / CW

  • L is the length of the needle (L = 30 pixels
  • N is the total number of needles (N = 50 needles)
  • C is the total number of needles crossing a line
  • W is the line spacing (Width of the wooden boards on the floor W = 40 pixels)

needle

Task 2


Adapt this Python script to automatically detect if a needle is crossing a line.

Your Python script should then count how many needles are crossing a line and use this to estimate a value of Pi.

unlock-access

Solution...

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

Euler’s Number

The number e is a famous irrational number called Euler’s number after Leonhard Euler a Swiss Mathematician (1707 – 1783). Number e is considered to be one of the most important numbers in mathematics.

The first few digits are: 2.7182818284590452353602874713527… It has an infinite number of digits with no recurring pattern. It cannot be written as a simple fraction.

Number e is the limit of (1 + 1/n)n as n approaches infinity:
euler-expression

Number e is a mathematical constant that is the base of the natural logarithm: the unique number whose natural logarithm is equal to one. Find out more on https://en.wikipedia.org/wiki/E_(mathematical_constant).

In this challenge we will use a Python script to calculate an approximation of e using three different approaches. The first approach as described above as already been implemented in the above Python trinket.

Calculating e using an iterative approach


Euler’s number, e, can also be calculated as the sum of the infinite series:

euler-infinite-series

Complete your Python script to implement this infinite series using an iterative approach. (Method 2)

Calculating e using a continuous fraction (iterative approach)


A less common approach to calculate number e is to use a continued fraction based on the following sequence:
euler-sequence
Continued Fraction:
euler-continued-fraction

Complete your Python script to implement this continued fraction. (Method 3)

unlock-access

Solution...

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

Golden Angle

golden-angleIn geometry, the golden angle is the smaller of the two angles created by sectioning the circumference of a circle according to the golden ratio.

Golden Angle ≈ 137.5°

The golden angle plays a significant role in the theory of phyllotaxis; for example, the golden angle is the angle separating the florets or branches on some plants or flowers (e.g. sunflower).

Plants have their leaves spread all around the stem to soak up as much sun as possible. Using the golden angle between two consecutive leaves is the most effective approach to spread the leaves around the stem.

Let’s see how this work:
Leaf 1:
golden-angle-1
Leaf 2: Would be at an angle of around 137.5° (The Golden Angle) from leaf 1
golden-angle-2
Leaf 3: Would be at an angle of around 137.5° (The Golden Angle) from leaf 2
golden-angle-3
And so on for all new leaves…
golden-angle-4
etc…
golden-ratio-animation

Python Challenge


For this challenge we will apply the golden angle to position the first 50 branches of a tree using Python Turtle:

golden-angle-8

Sunflower using Python Turtle and the Golden Angle


Tagged with: , ,

Graphing Algorithm

quadratic-equationIn this challenge we will write several procedures to graph different mathematical equations on screen using Python Turtle.

We will focus on:

  • Linear equations: y = ax + b
  • Quadratic equations: y = ax2 + bx + c
  • Trigonometric functions: e.g. y = cos(x)

(x,y) coordinates using Python Turtle?


xy-coordinatesOn the Python trinket used below the turtle screen is 400 pixels wide by 400 pixels high.

Look at the canvas below to understand how (x,y) coordinates work:

Linear Equation

y = ax + b

A linear equation produces a straight line when graphed on screen.

Your task:
Change the parameters used when calling the drawLine() procedure to graph the following equations:

  • y = 2x + 60
  • y = -x + 10
  • y = x
  • y = 1/2x

Quadratic Equation

y = ax2 + bx + c

Add another procedure called drawQuadraticEquation() that will take three parameters: a, b and c and be used to graph and display the matching quadratic equation.

Use this new procedure to graph the following equations:

  • y = x2 + 2x + 60
  • y = 1/2x2 – 3x + 20
  • y = x2
  • y = -x2 + 100

Trigonometric Functions

You can now add further procedures to draw the main trigonometric functions. To do so you will need to import the math library using an import statement.

import math

Here are the three main functions you will need to implement:

  • cosine: y = cos(x)
  • sine: y = sin(x)
  • tangent: y = tan(x)
Tagged with: , , ,

Monopoly Quiz

monopolyFor this challenge you will create a quiz based on the game of Monopoly.

The computer will randomly display a street name from the list of streets used in the game and the user will have to guess the colour used in the game for the selected street.

The user will score one point per correct answer and will be asked to guess the colour of ten different streets, one at a time. At the end the user will be given their score out of ten.

To get you started with this challenge we have created three text files to cater for different audiences:

TextFileUK-monopoly.txt
TextFileUS-monopoly.txt
TextFileFrench-monopoly.txt

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

Complete the Code


We have started the code for you but you will need to complete it to meet all the requirements of this game.

Extension Task #1


Add a menu option at the beginning to ask the user if they want to play with the UK, US or French monopoly.

Extension Task #2


Ask the user to enter their name and store their name and final score in a leaderboard text file. See leaderboard challenge.
unlock-access

Solution...

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

Equalizer Animation Using JavaScript

equalizerWhen 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.

In this challenge we are using JavaScript to create a frame-based animation. To do so we will use the setTimeOut() function to run our own javascript function called animate() every 50ms.

This is the line of code used to repeat the code used to refresh the equalizer every 50ms:

setTimeout(animate, 50);

Full Code

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

Your Challenge


Adapt this code to create a horizontal progress bar animation that expands from 0 to 100% progressively over 5 seconds. You can then make the animation loop by starting again at 0% when it reaches 100%.


unlock-access

Solution...

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