From Flowcharts to Python Code

recipeAn algorithm is like a recipe in a cook book. It is a step by step set of instructions that the computer will have to follow to solve a problem or complete a task.

sequencing-label
Algorithms consist of step by step instructions which are listed in order and will be executed in the same order, one instruction at a time: this is called sequencing.

iteration-label
On occasions a set of instructions needs to be repeated several times which is done in programming using a loop: this is called iteration.

selection-label
Computers also have to take decisions as to whether or not to run a set of instructions or to bypass these instructions. In programming these decisions are coded using IF statements: this is called selection.

Complex algorithms can use a range of sequencing, iteration and selection blocks.

To design an algorithm you can draw a flowchart or write pseudo-code.

Your algorithm (flowchart or pseudo-code) can then be converted by a programmer using the programming language of their choice (e.g. Python, Java, Visual Basic, etc.)

Coding Challenges


We have designed five algorithms (See flowcharts below). Your task is to implement each of these algorithms using Python code.

Stopwatch AlgorithmLeap Year?Winter OlympicsTimes TableLogin Form
A stopwatch is used to record the duration of an event as a number of seconds. Our stopwatch algorithm will convert/format this number of seconds to calculate and display the matching number of hours, minutes and seconds, knowing that:

  • 1 hour = 3600 seconds
  • 1 minute = 60 seconds

flowchart-chronometer

Python Code


Complete the code using the trinket below:

This algorithm is a good example of:
sequencing-label
Did you know that the next leap year will be in 2020? To find out if a year (e.g. 2020) is a leap year you can calculate the remainder of year (e.g. 2020) divided by 4. If this remainder is null, then the year is a leap year.

The following algorithm is used to test if a year is a leap year or not.
flowchart-leap-year

Python Code



This algorithm is a good example of:
selection-label
The last Winter Olympics took place in 2018 in Pyeong Chang (South Korea). Winter Olympics take place every 4 years. So when would be the next winter Olympics?

The following algorithm can be used to display the dates of the next ten Winter Olympics.
flowchart-winter-olympics

Python Code



This algorithm is a good example of:
iteration-label
The following algorithm will be used to ask the end-user to enter a number. The program will then display the full times table, from 1 to 10, for this number.
flowchart-times-table

Python Code



This algorithm is a good example of:
iteration-label
A login form should ask the user to enter their username and password. It will then check if these login details are correct before displaying either a welcome message or an error message.
flowchart-login

Python Code



This algorithm is a good example of:
selection-label

Extension Tasks


Use the same approach to complete the following “Flowchart to Python” challenges:

unlock-access

Solution...

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

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 40

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: