More results...

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

Magic 8 Ball – Using Python Blocks!

magic-eight-ballThe purpose of this post is to demonstrate how Python trinkets can be used to code with blocks.

Trinket blocks use the same approach as Scratch: A collection of blocks that the programmer can select to build their code in a more visual way.

The benefit of Trinket Blocks is that you can then view the Python code that it has generated.

A fantastic resource for those who are struggling with the syntax.

Also a great transition for those who have learned to program using Scratch and want to switch to text based programming e.g. using Python.

Look at this example that recreate the Magic 8 Ball program. Click on view code “>_” button to switch between the “Blocks code” and the Python code.

Tagged with: ,

Remove The Blocks – Robotics Challenge

tidyup3This is a classic robotics challenge where students have to build their own robot that will be used to tidy up a 2m by 1m mat full of lego bricks by either collecting or pushing these bricks outside the mat.

To complete this challenge we used the NXT Lego Mindstorm robot, alongside the Enchanting software to create the computer algorithm.

Here is our algorithm:

TidyUp2

You can find out more about the Enchanting software on: http://enchanting.robotclub.ab.ca/.

Tagged with: , ,

What’s my grade?

AGrade

Learning Objectives:

By completing this challenge you are going to further improve your skills at using:

  • Selection structures (using If statements),
  • Comparison operators (<, <=, >, >=, !=, ==)
  • Boolean logic (AND, OR) when using comparative operators

You may want to use our Python Cheat Sheet to complete this challenge looking especially at the section called “If Statements” and the section called “Comparison Operators”.

Your Challenge

Create a user friendly program that asks a user to input their exam score out of 60 marks. The program should output the grade that they have received using the grade boundaries listed in the table below.

Mark Grade
54+ A*
48 – 53 A
42 – 47 B
36 – 41 C
30 – 35 D
24 – 29 E
0 – 23 U


The program should also tell the user how many marks they are away from the next grade up.

Your Python code…



unlock-access

Solution...

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

Victor Vasarely’s Artwork revisited using Python

VasarelyIn this blog post we are looking at a specific painting from Victor Vasarely. The painting is from a serie of paintings called “Alphabet Plastique” and was painted in the 70’s.

We are going to try to recreate this painting using the turtle library in Python.

Did you know?


Victor Vasarely is a unique artist in the history of twentieth century art. Famous during his lifetime, he distinguished himself from contemporary art with the creation of a new movement: optical art. The evolution of his life of work is inherently coherent, progressing from graphic art to the artist’s determination to promote a social art that is accessible to all.
Source:www.fondationvasarely.fr

First attempt


Your Challenge


Search the internet for some artwork from Vasarely’s “Alphabet Plastique” collection and use this as a source of inspiration. Using the turtle library in Python (See script above), create your own piece of artwork inspired from your research.
Victor Vasarely - Selection

Tagged with: , , ,

Pixel Art in Python

smiley pixel art

Learning Objectives


In this blog post we are going to investigate how to use lists and list of lists with Python to create some 2D pixel art graphics.

List?


In Python, a list is used to save collection of values. For instance, to save all the different days of the week we could declare a variable called “daysOfTheWeek” and assign a list as follows:

daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

Based on this what do you think len(daysOfTheWeek) would return?
What’s about daysOfTheWeek[0]?
Or daysOfTheWeek[3] ?

Now look at this program used to display all the days of the week:

When using lists you can use a range of methods to add or remove values in the list. For instance:

Multi-dimensional Lists?


Sometimes you need more than on dimension to a list. In Python you can achieve this by creating a list of lists!

For instance for our pixel art project we want to store each pixel on a line within a list:
e.g.:

line1=[1,0,1,0,1,0]
line2=[0,1,0,1,0,1]
line3=[1,0,1,0,1,0]

We can then store all these lines into a list. Let’s call this list “grid”

grid=[line1, line2, line3]

A quicker way to do this is to do it all in one line as follows:

grid = [[1,0,1,0,1,0],[0,1,0,1,0,1],[1,0,1,0,1,0]]

We can then access any cell within this grid using the following command:

print grid[2][4] #This would return the value of the 5th of the 3rd line!

Let’s apply this to our pixel art project


Challenge #1:


Change this code to create the pixel art of a space invader:
spaceInvader

Challenge #2:


Tweak this code to allow colours to be used in your pixel art.
To do so you will first create a new list called colourPalette to store a collection of colours of your choice: e.g.

colourPalette = ["#FF0000#", "#00FF00", "#0000FF", "#000000", "#FFFFFF"]

More changes will be required for you to be able to recreate this pixel art:
Applepix

Traffic Lights Challenge

Did you know?

Traffic Lights
An embedded system is a computer system with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints.

A burglar alarm, an in-car cruise control system, a speedometer on a bike, a traffic lights control system are all examples of embedded system.

One application of computing is to create the algorithms used by these control systems. For this challenge we will focus on a traffic lights system.

Our Traffic Lights


Our system consists of two traffic lights used at a crossing.

The diagram below describes the sequence that these traffic lights have to follow:
traffic-light-sequence

We have started the code to control the first traffic light.

Your Challenge


Tweak this code to control the second traffic light. The second traffic light sequence should match the one described in the diagram above. However both traffic lights cannot be green at the same time!

Extension


Check this article to see how “smart” traffic lights help control the flow of traffic more effectively.

Tagged with: , ,

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