More results...

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

Flowchart to Python Code – Star Rating Validation

The aim of this Python challenge is to validate a user input using a range check.

In this program, the user will be asked to enter a star rating by entering a number value between 0 and 5. This could for instance be used to rate a movie (5 Stars = Excellent , 0 Star = Disappointing).

The program will check that the end-user has entered a number between 0 and 5 and if not, it will display an error message and repeat the question until a valid rating between 0 and 5 is entered.

Here is the flowchart for our algorithm:
flowchart-star-rating-validation

Python Code


You can now complete the Python code to implement this algorithm.

This algorithm is a good example of:
iteration-label

Testing


Now that your code is complete, it is important to test it to see if it is working as expected.

When testing a program, you can use different types of test and test data:

  • Valid Tests: To check that the program is working fine with acceptable data.
  • Extreme Tests: To test how your program works when you enter data that is at the limit (lower boundaries, upper boundaries) of what is considered acceptable data.
  • Erroneous Tests: To try to break the pogram, enter data that is not acceptable, that should be rejected and/or generate an error message.
Test # Type of Test Input Values Expected Output Actual Output
#1 Valid Star Rating: 2 Thank You
#2 Valid Extreme Star Rating: 0 Thank You
#3 Valid Extreme Star Rating: 5 Thank You
#4 Erroneous Star Rating: 9 Invalid Star Rating – Try Again
Enter a star rating between 0 and 5:
#5 Erroneous Star Rating: -2 Invalid Star Rating – Try Again
Enter a star rating between 0 and 5:
#6 Erroneous Star Rating: xyz Invalid Star Rating – Try Again
Enter a star rating between 0 and 5:

Extension Task


Did you all your test pass? If not, this means your code could be further improved.

Check this blog post to see if it could help you improve your code further and ensure all tests pass.

Adding validation routines when capturing user inputs is always a good approach to improve your programs and make them more robust.

You can investigate other forms of validation routines on this page.

unlock-access

Solution...

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

Flowchart to Python Code – Poker Dice Game

The aim of this challenge is to create a simplified game of Poker Dice using only three dice.
The computer will generate three random numbers between 1 and 6.

The program will then check to see if the three dice have the same value (“Three of a kind!”) or if any two of the three dice have the same value (“Pair”).

The game will be implemented using the following algorithm:
flowchart-poker-dice

Python Code

Use the above flowchart to help you write the Python code to solve this challenge.

This algorithm is a good example of:
selection-label

Extension Task


Could you improve your code further to find out if:

  • The 3 dice are all even numbers.
  • The 3 dice are all odd numbers.

Tip:
To find out if a die number is even you could check whether it is equal to 2, 4 or 6.

Pseudocode:

IF (die1==2 OR die1==4 OR die1==6) AND (die2==2 OR die2==4 OR die2==6) AND (die2==2 OR die2==4 OR die2==6) THEN
   DISPLAY("You have three even numbers.")
END IF

Another method would be to use [lists] and the keyword “in“:

Pseudocode:

IF die1 in [2,4,6] AND die2 in [2,4,6] AND die3 in [2,4,6]  THEN
   DISPLAY("You have three even numbers.")
END IF
unlock-access

Solution...

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

Flowchart to Python Code – Discount Price Calculator

discountShopping during the sales can sometimes be very confusing. With discounted prices at 10%, 20%, 50% or even 70%!

For this challenge you are going to write a Python script that prompts the user to enter a price in pounds (or in your own currency) (e.g. £90) and a discount rate to apply (e.g. 20%).

Your program will then calculate and display the discounted price.

We will use the following algorithm for our program:
flowchart-discount-price-calculator

Python Code


This algorithm is a good example of:
sequencing-label

Testing


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

Test # Input Values Expected Output Actual Output
#1 Price: £100
Discount Rate: 25%
Discount: £25
Discounted Price: £75
#2 Price: £160
Discount Rate: 40%
Discount: £64
Discounted Price: £96
#3 Price: £180
Discount Rate: 10%
Discount: £18
Discounted Price: £162
Tagged with:

Flowchart to Python Code – Temperature Converter

Fahrenheit-CelsiusDegree Fahrenheit (°F) and Degree Celsius (°C) are the main two units to measure temperature.

The Fahrenheit scale is used mainly in the USA whereas other countries tend to use the Celsius scale.

It is possible to convert a temperature from Celsius degrees to Fahrenheit and vice-versa using the following conversion formulas:
fahrenheit_to_celsius_formulas

Your Challenge


Use the following flowchart to write a Temperature Converter in Python. Your script will:

  • Ask the end-user whether they want to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius,
  • Ask the user to enter a temperature in the correct unit,
  • Apply the conversion formula and display the temperature in the new unit.

flowchart-temperature-conversion

Python Code


This algorithm is a good example of:
selection-label

Testing


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

Test # Input Values Expected Output Actual Output
#1 Option 1
10°C
50°F
#2 Option 1
0°C
32°F
#3 Option 1
-5°C
23°F
#4 Option 2
50°F
10°C
#5 Option 2
32°F
0°C
#6 Option 2
23°F
-5°C
#4 Option x Invalid option!
unlock-access

Solution...

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

Minecraft – Crafting Table

pickaxeFor this blog post we have recreated the crafting table in Minecraft using HTML, CSS and JavaScript.

This crafting table enables you to pick up items from your inventory to recreate some of the key recipes to craft new items and add these new items to your inventory.

Our version of the inventory is simplified as it does not record the quantity of items you have left. For each item listed in your inventory it is assumed that you have an infinite supply.

The purpose of this challenge is for you to investigate and reverse engineer how this code works.

First you may want to try this code with a few recipes:
recipe-Wood-Plank
recipe-Stick
recipe-swords

HTML, CSS and JavaScript Code

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


Your Task


You can then adapt this code to add a few more recipes such as the recipe to craft a compass:
recipe-Compass
And the recipe to craft a clock:
recipe-Clock

Note that you will find here a full list of Minecraft items.

For more crafting recipes, check this page.

Extra Challenge


The next step would be to tweak this code further to display and update the quantities available for each item in the inventory. These quantities should be reduced when items are used to craft new items, and increased when newly crafted items are added to the inventory.

Data Visualisation Algorithms

Data visualisation algorithms are used in most software (or video games) which are based on a Graphical User Interface.

They are used to provide a more intuitive, user-friendly visual representation of data.

There is a wide range of techniques and algorithms used to represent data in a visual way, often using Maths concepts (2D or 3D Coordinates, Trigonometry, Proportionality etc.)

The purpose of this blog post is to give examples of a range of data visualisation algorithms.

Example #1: Number of Lives


In a video game, a variable called numberOfLives would be used to store the number of lives remaining, as an integer.
e.g.

numberOfLives = 3

A basic algorithm could then be used to represent this visually at the bottom or top of the screen:
number-of-lives

Example #2: Speedometer


Another variable that can be used in a car racing game is the speed of the car in mph.
e.g.

speed = 60

A more advanced algorithm would then be used to represent this value on a speedometer:
speedometer-visualisation

Example #3: Analogue Clock


A similar algorithm can be used to represent the time using an analogue clock.
clock-visualisation
See Python Turtle Clock blog post to implement this algorithm using Python Turtle.

Example #4: Progress Bar


Using linear proportionality we can represent a numerical value as a bar or column.

These three examples would use such an approach to represent data visually:

progress-bar-visualisationProgress Bar
thermometer-visualisation
Thermometer
equalizer-visualisation
Equalizer

See this “Equalizer Animation” blog post to implement this algorithm using HTML, CSS and JavaScript.

Example #5: Pie Charts, Bar Charts, Line Charts, Radar Charts…


Charts are often used to represent data visually. Most high level languages have libraries that can be reused to draw charts.
charts-data-visualisation
Try this blog post to draw your own charts using Python Turtle.
Obviously, Python Turtle is not the best library to create charts. Your next step, should you wish to produce advanced charts is to investigate the matplotlib Python library which is very popular to create a wide range of charts. Find out more:

Infographics use a wide range of charts to represent data in a visual way.
infographic-data-visualisation

Example #6: 2D Representation


Most of the first arcade games where based on a 2D interface.
space-invaders-pixels

A 2D interface can be used to represent 2D arrays:

data-visualisation-thatre-booking-systemTheatre Booking System

2D data visualisation algorithms can also be used to represent graphs data structures:
tubemap

Try the following blog posts to visualise 2D arrays on screen:

Example #6: 3D Representation


Today video games tend to use a 3D user interface which involves more complex data visualisation algorithms.
3d-data-visualisation

Try the following blog posts based on 3D data visualisation:

mine-craft-block

Example #7: Adding Animation


Data visualisation algorithms can be made more complex when there is a need to animate the visual representation of the data!

Try the following algorithms to create animated simulations:

More examples of Data Visualisation


The following websites contain a wide range of data visualisation techniques:

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
Tagged with:

Time Conversion Algorithm

clock

Did You Know?


The 12-hour clock is a time convention in which the 24 hours of the day are divided into two periods: a.m. (from the Latin ante meridiem, meaning “before midday”) and p.m. (post meridiem, “after midday”). Each period consists of 12 hours numbered: 12 (acting as zero), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11. The 24 hour/day cycle starts at 12 midnight (often indicated as 12 a.m.), runs through 12 noon (often indicated as 12 p.m.), and continues to the midnight at the end of the day.

time-conversion-24-12-am-pm

24-hour to 12-hour clock convertor


Your challenge consists of writing a computer program that asks the end-user to enter a time in the 24-hour format (e.g. 18:36). The program will convert this time in the 12-hour format (e.g. 6:36 PM).

To do so you can base your Python code on the following flowchart:
time-conversion-flowchart

Python Code


Testing


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

Test # Input Values Expected Output Actual Output
#1 06:30 6:30 am
#2 14:25 2:25 pm
#3 00:52 12:52 am
#4 11:59 11:59 am
#5 12:00 12:00 pm
#6 23:59 11:59 pm

Extension


warning-signAmend this code to validate the user entry and make sure they enter a time between 00:00 and 23:59 and to display a meaningful error message if not.


unlock-access

Solution...

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

A Level Computer Science – Connect Wall

Boost your A Level Computer Science revision by completing this Connect Wall activity!

A Level Computer Science – Connect WallOpen this activity in a New Window

Tagged with:

GCSE Computer Science – Connect Wall

Boost your GCSE Computer Science revision by completing this Connect Wall activity!

GCSE Computer Science – Connect WallOpen this activity in a New Window

Tagged with:
Privacy Overview

This website will store some information about your preferences on your own computer inside a tiny file called a cookie. A cookie is a small piece of data that a website asks your browser to store on your computer or mobile device. The cookie allows the website to remember your actions or preferences over time.

You can delete all cookies that are already on your computer, and you can set most browsers to prevent them from being placed. However, if you do this, you may have to manually adjust some preferences every time you visit a site, and some services and functionalities may not work.

Most browsers support cookies, but you can set your browser to decline them and can delete them whenever you like. You can find instructions here for how you can do that on various browsers.

This website uses cookies to:

  1. Identify you as a returning user and to count your visits in traffic statistics analysis
  2. Remember your custom display preferences (such as light/dark theme option)
  3. Provide other usability features, including tracking whether you have already given your consent to cookies

Enabling cookies is not strictly necessary for the website to work but it will provide you with a better browsing experience.

The cookie-related information is not used to identify you personally and is not used for any purpose other than those described here.

There may also be other types of cookies created after you have visited this website. This site uses Google Analytics, a popular web analytics service that uses cookies to help to analyse how users use the site. The information generated by the cookie about your use of this website (including your IP address) will be transmitted to and stored by Google on servers in the United States. Google will use this information for the purpose of evaluating your use of other website, compiling reports on website activity, and providing other services relating to website activity and internet usage. Google may also transfer this information to third parties where required to do so by law, or where such third parties process the information on Google’s behalf. Google undertakes not to associate your IP address with any other data held by Google.