More results...

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

Original Price Calculator

sale-labelsShopping 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 discounted price in pounds (or in your own currency) (e.g. £90) and the discount rate that this price benefits from (e.g. 25%).

Your program will then calculate and display the original price (price before discount) of the item (e.g. £120).

price-after-discount

Flowchart


Your program will consist of 3 key sections: Input > Process > Output
input-process-output

The flowchart of your program is as follows:
Price-Before-Discount-Flowchart

Python Code


Your task is to complete the following Python code:

Testing


You can now use your code to complete the following test plan:

Test # Input Values Expected Output Actual Output
#1 Discounted Price: £90
Discount Rate: 25%
Original Price: £120
#2 Discounted Price: £45
Discount Rate: 40%
Original Price: £75
#3 Discounted Price: £210
Discount Rate: 30%
Original Price: £300

discounted-prices

unlock-access

Solution...

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

Python Turtle – Protractor Challenge

Python Turtle? Let’s Recap!


Looking at the following code can you explain the purpose of each Python Turtle instructions:

  • turtle.color(“red”)
  • turtle.forward(100)
  • turtle.right(90)
  • turtle.left(45)
  • turtle.penup()
  • turtle.pendown()
  • turtle.goto(0,0)
  • turtle.circle(50)
  • turtle.setHeading(45)

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

Protractor Challenge:


A protractor is an instrument used in Maths when measuring or drawing angles.

Our challenge is to use Python to draw a protractor on screen, using all the graduations for a scale going from 0° to 180°.
protractor

To do so we will use an iterative algorithm based on the following flowchart:
protractor_flowchart

Your task consists of using Python code to implement this flowchart using Python Turtle.

Extension Task #1


Improve the drawing of your protractor further by:

  • adding labels (e.g. 0°, 10°, etc.) next to each graduation,
  • adding smaller graduations (e.g. 5°, 15°, 25° etc. graduations).

Extension Task #2


Use a similar approach to draw an analogue clock using Python Turtle.
unlock-access

Solution...

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

Area Calculator Flowchart Challenge

In this challenge you will design an algorithm to calculate the area of various shapes as listed below:

Shape Name Area
area-square Square width2
area-rectangle Rectangle width x length
area-circle Circle pi x radius2
area-triangle Triangle base x height / 2

Your algorithm should:

  • Ask the user which of the above four shapes they would like to calculate the area of,
  • Based on the chosen shape, your algorithm should ask the end user to enter the required dimensions (e.g. width, or width and length or radius or base and height),
  • Calculate and display the area of the chosen shape.

Flowchart Challenge


Use our flowchart desing tool to create the flowchart of your algorithm.

Online Flowchart Creator

Python Challenge


Once you have completed your flowchart, you can implement this algorithm using Python code.

Test Plan

Test # Input Values Expected Output Actual Output
#1 Square
Width: 5
Area: 25
#2 Rectangle
Width: 4
Length: 6
Area: 24
#3 Circle
Radius: 10
Area: 314.159
#4 Triangle
Base: 7
Height: 4
Area: 14

Extension Task


Complete your algorithm to cater for additional shapes including parallelograms, rhombus and hexagons.
unlock-access

Solution...

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

Min, Max, Mean, Median and Mod – Flowcharts

In this challenge we will design algorithms used to calculate the Min, Max, Mean, Median and Mod from a list of numbers.

First you may want to refresh your maths skills and check the meaning of these terms: http://www.purplemath.com/modules/meanmode.htm

MIN Flowchart


min-flowchart

Challenge #1


Use our flowchart designer tool to create 4 more flowcharts to:

  • Calculate the Max value of a given list,
  • Calculate the Mean value of a given list,
  • Calculate the Median value of a given list,
  • Calculate the Mod value of a given list.
Online Flowchart Creator

Challenge #2


Now that you have designed your algorithms you can implement these using Python 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:

Fizz-Buzz Game Algorithm

fizz-buzz-iconFizz-Buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any multiple of three with the word “fizz”, and any multiple of five with the word “buzz”.

Fizz-Buzz Challenge


For this challenge you need to write a computer program that will display all the numbers between 1 and 100.

  • For each number divisible by three the computer will display the word “fizz”,
  • For each number divisible by five the computer will display the word “buzz”,
  • For each number divisible by three and by five the computer will display the word “fizz-buzz”,

This is what the output will look like:

    1
    2
    Fizz
    4
    Buzz
    Fizz
    7

Fizz-Buzz Algorithm


Check the following flowchart used to generate the first 100 numbers using the Fizz-Buzz rules. (Click on the flowchart to enlarge it).
flowchart-fizz-buzz

Python Challenge


Use the above flowchart to complete this Python code.

Extension Task #1


Tweak this code to use the input command in Python. The program should ask the user to input numbers in order or type the word Fizz or Buzz when required. The program should check that the user has typed the right number or the right word and stop if not. It should then give the final score (How far the player completed the game).

Extension Task #2


Improve this code further to give the players three lives. The player can make up to 3 mistakes. Each time they make a mistake they lose 10 points but carry on playing till they lose their three lives.

Finding the factors of…

For this challenge you will use an algorithm to find all the factors of a given number.

Factors are numbers we can multiply together to get another number. For instance, factors of 15 are 1, 3, 5 and 15, because 1×15=15 and 3×5 = 15.

Your algorithm will be based on the INPUT – PROCESS – OUTPUT model:

  • INPUT: Ask the user to enter a number,
  • PROCESS: Identify all the factors of this number,
  • OUTPUT: Display all the factors of this number.

Flowchart


flowchart-factors-of-a-number

Task 1: Python Code


Your task is to implement your algorithm using Python code.

Task 2: Test Plan

Test # Type of Test Input Values Expected Output Actual Output
#1 Valid 12 1,2,3,4,6,12
#2 Valid 21 1,3,7,21
#3 Valid 48 1,2,3,4,6,8,12,16,24,48
#4 Valid 13 1,13

Task 3: Extension Task: Prime Number?

A prime number is a number that has exactly two factors (1 and itself). Which means that a prime number can be divided evenly only by 1, or itself. A prime number must be a whole number greater than 1.

How could you tweak this code to detect if a number is a prime number or not. If a number is a prime number your algorithm should display a message saying so.

Task 4: Extension Task: Simplifying a Fraction


Complete the following two challenges:

unlock-access

Solution...

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

Bidding Process – Flowchart

auction-bidFor this challenge we will consider the bidding process used within online auction websites such as eBay.

Our aim is to create a system that will accept bids from the end-user, check if the bid being placed is greater than the current bid and if so, update the value of the current bid.

Here is the flowchart for our Bidding System: (Click on this flowchart to open in new window)
ebay-flowchart

Making Predictions


Check the above flowchart to understand how this algorithm will work. Based on this flowchart can you answer the following questions?


?
What will be the first question asked to the end user?


?
How does this algorithm decide if a bid is a valid bid or not?


?
How does this algorithm decide when the bidding process ends?


?
What will be the last message displayed to the end-user?

Python Challenge


Your challenge is to use Python code to implement this algorithm.

Extension Task


Check the following two blog posts to see how this code could be improved further by adding validation routines when capturing user inputs. Use these techniques to make your bidding system more robust:

unlock-access

Solution...

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

Tagged with:

Entry Fees Calculator using a Flowchart

For this challenge you will create a flowchart to explain the process that a computer will have to follow to calculate the entry fees to an aqua park for a small group of visitors or a family.

Below is the price list of the aqua park:
Aqua-Park-Price-List

Video Tutorial

Watch the video tutorial for step by step instructions on how to complete this challenge.

Task 1: Design the Flowchart


First you will design the flowchart for your algorithm. The aim is your algorithm is to:

  • Ask the user how many adult tickets are needed,
  • Ask the user how many child tickets are needed,
  • Calculate the total cost of this order,
  • Decide if this order qualifies for a 5% discount and if so, calculate the new total cost of the order,
  • Output the total cost of the order.
Online Flowchart Creator (using Google Chrome)

Task 2: Python Code


Once your flowchart is complete, implement your algorithm using Python code.

Task 3: Test Plan

Test # Type of Test Input Values Expected Output Actual Output
#1 Valid Adults: 2
Children: 1
Total Cost: £41
#2 Valid Adults: 1
Children: 0
Total Cost: £15
#3 Valid Adults: 2
Children: 3
Total Cost: £59.85
#4 Valid Adults: 5
Children: 12
Total Cost: £196.65
#5 Valid Extreme Adults: 0
Children: 0
Total Cost: £0
#6 Erroneous Adults: abc
Children: xyz
Error Message

Task 4: Adding extra validation routines

Tweak your code to add some validation routines when capturing user inputs. For instance, you want to make sure that the user only enters a positive integer value when asked for the number of tickets required.

You might find this blog post useful to ensure the user provides an integer value when asked to do so.

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 Creator

A flowchart is a visual diagram used to describe an algorithm. It enables you to describe the key steps of your algorithms and highlights when the user will be required to input data, when the computer will output/display information to the end-user, when a decision block (Selection / IF Statement) or a loop (Iteration) is used.

A flowchart uses specific shapes including:

  • Oval shapes to represent the START and END of an algorithm,
  • Parallelogram shapes to represent interactions with the end-user (INPUT and OUTPUT),
  • Rectangle shapes to represent a process or a calculation,
  • Diamond shapes to represent an IF Statement (Selection) or a LOOP (iteration).

Flowchart Task


Try our new online flowchart creator to design your own algorithms.

Recommended Browser: Google Chrome.

To familiarise yourself with this tool, try to reproduce the following flowchart:
star-rating-validation-flowchart

Online Flowchart Creator
Tagged with:

Stacks and Queues using Python

queue-road-signStacks and Queues are two key data structures often used in programming.

A queue is a FIFO data structure: First-In First-Out in other words, it is used to implement a first come first served approach. An item that is added (enqueue) at the end of a queue will be the last one to be accessed (dequeue).

A stack is a FILO data structure: First-In Last-Out. Imagine a stack of books piled up on a table. When you add (push) a book on top of the pile, it will be the first book that you will then take (pop) from the pile (stack).

queue-diagram

stack-diagram

Both stacks and queues can easily be implemented in Python using a list and the append(), pop() and remove() functions.

In the following trinket we are creating our own classes, Queue and Stack. Using Object Oriented Programming (OOP) enables us to prevent direct access to the list (defined as a private attribute – This is an example of encapsulation a key concept in OOP). It also enables us to create and name our own methods, pop(), push(), enqueue() and dequeue() to match the terminology used when using stacks or queues.

Your Task…


Update the above Stack Class and Queue Class to have an extra private property called maxCapacity (positive integer value). This property should be initialised based on a parameter passed using the constructor of each class.

The code to push a value to the Stack or enqueue a value to the queue should be amended to only push or enqueue the value if the stack or the queue have not reached their maximum capacity.