More results...

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

Python Shapes using Turtle

Learning Objectives

In this challenge we are going to use x and y coordinates to draw shapes on the screen. We will be using the turtle library to draw on the screen.

X and Y coordinates? Quadrant?


xy-coordinates
Check the above picture. Can you state three facts about X and Y coordinates and about quadrants?

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:

Challenge #1

Look and test the following code used to draw a “+” shape.

Your task is to tweak this code to draw the following shapes:
PythonShapes

Challenge #2

Look and test the following code used to draw a square using the for loop.

Your task is to tweak this code to draw the following shapes:
PythonShapes2

Extension


Once you have completed all the shapes above you can try this more advanced challenge.

Tagged with: , ,

My E-Mail Validation Script

The aim of this challenge is to write a computer program that asks the end-user to type their e-mail address. The program should decide whether the e-mail being entered is a valid one or not.

Some of the validation checks that your program should complete are as follows:

  • The email should contain one and only one “@” sign
  • The email should contain at least one “.” sign located after the “@” sign. However it may contains more than one “.” sign for emails ending in .co.uk for instance
  • The email cannot contain any space or “#” signs
  • The “@” sign cannot be in the first position and there should be at least 1 character between the “@” and the “.” sign.
  • The email cannot end with a “.” sign

Can you think of any other rules to make your validation script even more robust?

Learning Objectives

By Completing this challenge you are going to develop your string manipulation skills!

Let’s Get Started

Look at the following code used to find out if the email contains an “@” sign.

Comlete this code to implement all of the validation check mentionned above.

unlock-access

Solution...

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

Fancy a game of Poker Dice?

diceThe 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 user scores points as follows:

  • Three of a kind: +50pts
  • Straight (e.g. 3,4,5): +30pts
  • Three odd numbers: +15pts
  • Three even numbers: +15pts
  • One pair: +10pts

Step 1: Using random numbers

Start your game by generating three random numbers and displaying these to the end user.
You will need to import the randint function from the random python library. To do so you can use the following code:

from random import randint

#to generate a random number between 1 and 10 use the following instruction:
myNumber = randint(1,10)

Step 2: Odd or Even?

Think about the characteristic of an odd number aka a number which cannot be divided evenly by 2.

Using the mod (remainder: % sign in Python) we can determine if a number can be divided evenly by 2.

Based on this we can create a function (sub routine) called isOdd that will take a number as a parameter and return True if this number is odd, False if not.

Try this yourself or check the following code.

def isOdd(number):
    if (number % 2 == 1):
    	return True
    else:
        return False

Can you make up another function to findout if a number is even?

Step 3: Complete the game…

You will need to reuse the two functions, isOdd() and isEven() that you created in the previous step.

Complete the code

Check the code below and complete it further:

unlock-access

Solution...

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

Prime Number Check in Python

Did you know?

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.

2, 3, 5, 7, 11, 13, 17… are all examples of prime numbers.

Your challenge:

Write a program that prompts the user to enter a number between 1 and 9999. The program will inform the end-user whether their number is a prime number or not.

Hint:

In Python you can easily calculate the remainder of a division using the % sign.

For instance 7 % 2 would return 1.

So to check if a number “a” can be divided evenly by another number “b” you can check if the remainder of a/b is null using if a%b==0

Solution

So let’s look at the following code used to check if a number is a prime number or not.

Extended Challenges

  1. Tweak this code to display all the prime numbers between 1 and 9999.
  2. Write another program that takes a user input and find out if the number being entered is either odd or even.
unlock-access

Solution...

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

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: