More results...

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

Barcode Generator – Using Python

barcodeFor this challenge we are investigating how barcodes are produced. Though there are different standards to produce barcodes, they all follow a similar approach based on binary code!

Did you know?

On a barcode the pattern made using black and white lines is in fact binary code! A black line represents a 1 and a white line a 0.

So the following binary code “0101” consists of a thin white line, a thin black line, a thin white line and a thin black line.

The following binary code “1110” however consists of a large black line (as thick as 3 lines) and a thin white line.

So to produce our own simplified bar codes all we need to know is convert each digit of the actual number into binary (using 4 digit binary codes, aka a nibble)

Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

Using Python to generate random barcodes

Your Challenge

Adapt this script to ask the end-user to enter their initials. Use the ASCII table to convert these initials into ASCII and then into binary code to then generate a barcode matching those initials.

Tagged with:

Random Cloud Generator using Python

cloudCan computers produce artwork? In this challenge we are looking at how to write a piece of code to generate a “pretty” graphic.

Our first script will generate a random cloud on a blue sky by drawing several (15 in total) white circles of different sizes and position on the canvas. It uses the randomize library to randomly alter the size and position of these circles.

Your Challenge


Use a similar script to generate a random forest as follows:

forest

Extended Challenge


How could you change your script so that each tree has a different shade of green:

forest2

Tagged with: ,

Recursive vs. Iterative Algorithms

The purpose of this blog post is to highlight the differnce between two types of algorithms: Iterative and Recursive algorithms.

The challenge we will focus on is to define a function that returns the result of 1+2+3+4+….+n where n is a parameter.

The Iterative Approach

The following code uses a loop – in this case a counting loop, aka a “For Loop”.
This is the main characteristic of iterative code: it uses loops.

# iterative Function (Returns the result of: 1 +2+3+4+5+...+n)
def iterativeSum(n):
    total=0
    for i in range(1,n+1):  
        total += i
    return total

The Recursive Approach

The following code uses a function that calls itself. This is the main characteristic of a recursive approach.

# Recursive Function (Returns the result of: 1 +2+3+4+5+...+n)
def recursiveSum(n):
    if (n > 1):
        return n + recursiveSum(n - 1)
    else:
        return n

You can visualise/trace this recursive function on recursivevisualizer.com

Let’s see both approaches in action

Your Challenge:

Tweak both functions above to:

  • add up only even numbers: e.g. 2+4+6+8+….+n-2+n
  • add up only odd numbers: e.g. 1+3+5+…+n-2+n
  • add up numbers, counting in 5: e.g. 5+10+15+…+n-5+n

Using CSS to organise text into columns

To improve the readability and design of your web pages you may want to organise your text into multiple columns.

See the Pen CSS Columns by 101 Computing (@101Computing) on CodePen.

Your Challenge


Change the above code to split the text in two columns instead of three.

Responsive Columns


A responsive design is a design that adapts to the size of the screen used to display the webpage.

For instance, on a wide screen, splitting your text in three columns maybe appropriate, but the columns may look “skinny” when the page is displayed on a smaller screen. (Tablet, Smart Phone…)

One technique used to adapt the look and feel is to create CSS code for different sizes of screen using the following code:

@media only screen and (max-width: 480px) {
    /* STYLES HERE for BROWSER WINDOWS with a max-width of 480px. This will work on desktops when the window is narrowed.  */
}

You can see what this code will do by resizing your browser window. The text should switch between two or three columns based on the width of your browser window. (For the purpose of this test, you will need to press the “Edit on CodePen” button to display this content on its own window and then be able to resize the window).

See the Pen CSS Columns – Responsive Design by 101 Computing (@101Computing) on CodePen.

Your Challenge


Change the above CSS code to add another section where the text will only use one column for screens/browser windows with a width of 650px or less.

Projectile Motion Formula

Projectile-MotionMost artillery games are based on the Projectile Motion Formula used to trace the trajectory of a projectile thrown in the air. Due to gravity, its trajectory will be a parabola which shape will vary based on the angle and initial velocity of the projectile.

Use the script below and see what happens when you change the angle. (e.g. use a value between 0 and 90 degrees) or the velocity.

This page helped us with defining the equation for the trajectory of the projectile:
https://en.wikipedia.org/wiki/Projectile_motion

Using the displacement formula we can calculate the position ((x,y) coordinates) of a projectile at any given time.
projectile-motion-formula-t
In these formulas:

  • projectile-motion-formula-x0y0represent the starting position. (e.g. position of the tank on the screen),
  • projectile-motion-formula-v0 represents the initial velocity, in other words the initial power/speed that was used to shoot/throw the projectile,
  • projectile-motion-formula-theta (theta) represents the angle of projection. (At what angle was the projectile thrown)
  • projectile-motion-formula-time represents the time in seconds since the object was thrown. (Starts at 0). The number of frames since the object has been thrown can be used as a frame based game display a frame every x milliseconds.)
  • projectile-motion-formula-gravity represents the gravity. (On planet Earth: g = 9.81)

Let’s apply these formula using a Python script using the processing library to create a frame based animation.

Angry Birds, Tanks, Worms, Sports/Ball based games (Basketball…) all use a similar algorithm and formula. Can you think of any other video games based on this formula?

Alternative Approach

An alternative approach to implement this projectile motion formula to a flying object/sprite/ball using a frame based animation is to recalculate the position and velocity vector of the sprite at frame n+1 based at on its position and velocity at frame n:
velocity-vector

To do so we will use the following formulas:
projectile-motion-formula-frame
In these formulas:

  • projectile-motion-formula-v0 represents the initial velocity, in other words the initial power/speed that was used to shoot/throw the projectile,
  • projectile-motion-formula-theta (theta) represents the angle of projection. (At what angle was the projectile thrown)
  • projectile-motion-formula-time represents the time in seconds since the object was thrown. (Starts at 0). The number of frames since the object has been thrown can be used as a frame based game display a frame every x milliseconds.)
  • projectile-motion-formula-delta represents the “delta”: the amount of time (milliseconds) between two frames
  • projectile-motion-formula-gravity represents the gravity. (On planet Earth: g = 9.81)

Python Code:

Tagged with:

Music Score using Python

Music-ScoreIn this challenge we are going to use Python to draw a music score by positioning notes on the staff.

Look at the following code:

Your challenge:


Tweak the code given above so that the user can enter several notes at the same time. For instance the user should be able to type “CABFFE”. The program should display all six notes on the staff.

By completing this challenge you will further improve your string manipulation techniques in Python.

Tagged with:

Goal Line Technology

Goal Line Technology

Goal Line TechnologyInspired by the football worldcup we came up with our own Python script to implement the goal line technology.

Rules of the challenge:

  • Use Python to display the goal line on the screen as well as the two goal posts.
  • Every time the program is executed it should draw the ball on a random position on the screen.
  • The program should decide if there is a goal or not. There is a goal if the ball has passed the goal line and is between the two goal posts.
  • Tip: You will need to use the random Python library to generate random coordinates for the ball.

    Our first attempt using Python Turtle


    Our second attempt using the processing library


    This version is slightly different. The ball follows the mouse pointer. It turns green if you score a goal or red if the ball is out.

    You will find everything you need to know about the Processing library using this website and you can look at some of these examples to see how it is used.

    Read more

    You can do some research on the Internet to find out how the goal line technology really works. You will find out that there are different types of systems as explained on this page about goal line technologies.

    Tagged with: ,

    Python Turtle Clock

    This challenge consists of using the Python turtle library and the datetime library to create a program that displays the current time as an anlogue clock.

    To complete this challenge we need to do some angle calculations to understand what angles to use when displaying the small hand (hours) and the big hand (minutes).

    clock

    Let’s do some calculations…

     

    90_degree_rotations
    What angle should you use for the small hand when it is:

    • 3:00 AM
    • 6:00 AM
    • 7:00 AM
    • 3:00PM
    • 5:00PM

     

    What angle should you use for the big hand when it is:

    • 3:30 AM
    • 3:45 AM
    • 3:05 AM

    Task 1:

    Customise your clock by adding graduations (see picture below) and complete the code to add a third hand for the seconds (between 0 and 60) using a green hand.

    tutrle-clocks

    Task 2:

    In reality the calculation for the small hand’s angle is slightly more complex. For instance when it is 2.45PM the small hand should not be on 2 o’clock but somewhere between 2 and 3 o’clock.

    This wikipedia page gives you the exact calculation that you should use to calculate the angle of the small hand.

    Your task is to update the above Python code to take into consideration this new angle calculation.

    ClockAngles

    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 Challenge

    In this challenge we are going to use the turtle library to draw the following shape:
    Python-Turtle-Shape-Complete

    Step 1: Run the code below:

    Step 2: Complete the code to draw the full shape. (You can complete the code in the Trinket widget above).

      Python-Turtle-ShapePython-Turtle-Shape-Complete

    Hint?


    To complete this challenge you need to consider the differences between each of the 4 quadrants:
    xy-coordinates
    unlock-access

    Solution...

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

    Google Self-Driving Car: Flowchart

    Google Self-Driving car is controlled by computer algorithms.

    In our previous post we looked at how to create a self-parking robot using Lego NXT and Enchanting software.

    For this challenge all we will need is a pen and some paper, or if you prefer you can use https://www.draw.io/ to draw a flowchart online.

    Initial Investigation

    Firstly, read this article or watch the video clip about the Google Self Driving Car. This article will appear with a set of questions for you to think about how the Google car works.

    Your Challenge

    Let’s look at the crossroads with traffic lights scenario. Using https://www.draw.io/ create the flowchart or pseudocode for the computer algorithm of the Google Self-Driving car, knowing that the SatNav of the car is advising the car to turn right at this junction.

    The following picture may help you get started. However you will most likely need extra blocks to cater for all possibilities.GoogleCarFlowchart