More results...

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

Python Turtle Race

In this challenge we will create a Python script to complete a Turtle race with four turtles racing across the screen.

We have started the code for you to set up the pitch of grass. You will complete this project step by step in order to:

    Add a start line to the left of the screen,
    Add a finish line to the right of the screen,
    Add four turtles on the start line,
    Animate the turtles to race across the screen,
    Find out which turtle is first to cross the finish line

Here is the Python code that you will need to complete:

Step 1: Start lineStep 2: Finish lineStep 3: Adding TurtlesStep 4: Race Time!Step 5: And the winner is...

Step 1: Adding a start line


To draw the start line as a vertical line on the left hand side of the screen, we will need to use the following (x,y) coordinates to tell the computer where does the start line starts and ends:

You will can add the following code to the above trinket to draw the start line:

#Let's draw the start line
myPen.penup()
myPen.goto(-160, 150)
myPen.pendown()
myPen.goto(-160, 0)

Step 2: Adding a finish line


It’s now your turn to use very similar code, this time to draw the finish line using the following (x,y) coordinates:

View Solution!

Step 3: Adding four turtles on the start line

We are now going to add the four competitors on the start line. We will have four turtles that we will need to position using the following (x,y) coordinates:

Here is the code to add the red turtle at position (-180, 140):

#Let's add our turtles
redTurtle = turtle.Turtle()
redTurtle.shape('turtle')
redTurtle.color('red')
redTurtle.pensize(2)
redTurtle.penup()
redTurtle.goto(-180, 140)
redTurtle.pendown()

Can you add some more lines of code to add 3 more turtles using the following coordinates:

  • Green Turtle at position (-180,110)
  • Blue Turtle at position (-180,80)
  • Yellow Turtle at position (-180,50)
View Solution!

Step 4: Animating the turtles to race across the screen


It’s now time for our turtles to race across the screen. To do so we will use an infinite while loop. Within each iteration of the loop, we will move each turtle forward by a randomly generated number of pixels between 1 and 8.

Here is the code to use for our infinite loop…

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))

Step 5: And the winner is…

As you can see with step 4, the four turtles carry on racing even after crossing the finish line. They even disappearing off screen!

Our aim is now to check, within each iteration of the while loop, to see if one of the four turtles has reached the finish line. To do so, we will need to check the x coordinate of each turtle. A turtle with an x coordinate greater than 180 has crossed the finish line!

Here is the final code for our racing loop!

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))
  
  if redTurtle.xcor()>180:
    print("Red Turtle wins!")
    break
  elif greenTurtle.xcor()>180:
    print("Green Turtle wins!")
    break
  elif blueTurtle.xcor()>180:
    print("Blue Turtle wins!")
    break
  elif yellowTurtle.xcor()>180:
    print("Yellow Turtle wins!")
    break
unlock-access

Solution...

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

Crenellation Challenge

In a medieval castle, the crenelation (a.k.a battlement) is a parapet at the top of a wall that has regularly spaced squared openings for archers to shoot through.

In this challenge, we are going to use Python Turtle to create repeating patterns such as the crenellation of a medieval castle.

First, let’s investigate how to draw a horizontal line using python Turtle:

Task #1:

Your task is to edit the above code to create a single crenellation pattern as follows: (Instead of a horizontal line going across the screen).

To do so you will need to use a few Python Turtle instructions including:

  • myPen.forward(20)
  • myPen.left(90)
  • myPen.right(90)
View Solution!

Task #2: Repeating Pattern


In order to repeat this crenellation pattern to go across the screen, we will use a for loop.

for i in range (0,10):
    myPen.left(90)
    myPen.forward(15)
 
    myPen.right(90)
    myPen.forward(20)

    myPen.right(90)
    myPen.forward(15)

    myPen.left(90)
    myPen.forward(20)

Task 3: Alternative Crenellation Patterns…

Your task is to tweak the above code to recreate the following repetitive patterns…



unlock-access

Solution...

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

Zip It! – Python Challenge

For this challenge, we are going to recreate an existing built-in python function called zip().

The purpose of the zip() function in python is to merge two given lists into a single list of tuples. Each tuple will contain two values: one value from each list being merged.

You can see how the zip() function works using the trinket embed below.

Your task is to create a new function called zipLists() that will behave exactly the same way as the built-in zip() function, using your own algorithm. Your function will need to take two lists as parameters and return a “zipped list of tuples”. To start with, you may assume that both lists provided will be of the same length (contain the same numbers of values). You will then be able to adapt your code to consider the case where one list has less values than the other. In this case you will ignore surplus values.

Python Code


Complete the code below to define and test your own zipLists() function.

unlock-access

Solution...

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

Join the Dots Challenge

To complete this challenge, your task is to connect the 9 dots below using just 4 connected straight lines (without lifting your pen!).

Grab a piece of paper and have a go.

Alternatively, you can draw and connect the 9 dots on this online whiteboard:

Python Turtle Challenge


Did you manage to connect the 9 dots in just 4 straight lines, without lifting your pen? If so you are now going to write an algorithm using Python Turtle for the computer to solve this challenge by following the instructions of your algorithm. To do so we will use (x,y) coordinates. The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high. The coordinates (0,0) refers to the centre of the cnavas.

xy-coordinates

Look at the canvas below to understand how (x,y) coordinates work:

In order to draw on screen, we will use the goto() instruction from the turtle library. This instruction takes two parameters: the x,y coordinates of where the pen should travel to:

pen.goto(50,150)

Complete the code…

We have started the code for you. Your task is to add a few goto() instructions for the computer to solve this challenge challenge!

Tagged with:

The Envelope Challenge

To complete this challenge, your task is to draw the following envelope without lifting your pen and without going over the same line twice!

Grab a piece of paper and have a go.

Alternatively, you can draw your envelope on this online whiteboard:

Python Turtle Challenge


Did you manage to draw the envelop in one continuous line, without visiting any segment twice? If so you are now going to write an algorithm using Python Turtle for the computer to solve this challenge by following the instructions of your algorithm. To do so we will use (x,y) coordinates. The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high. The coordinates (0,0) refers to the centre of the cnavas.

xy-coordinates

Look at the canvas below to understand how (x,y) coordinates work:

In order to draw on screen, we will use the goto() instruction from the turtle library. This instruction takes two parameters: the x,y coordinates of where the pen should travel to:

pen.goto(50,150)

Complete the code…

We have started the code for you. Your task is to add a few goto() instructions for the computer to solve the envelope challenge!

Extension Task


Your extension task consists of drawing this diamond shape using Python Turtle. Note that it, is not possible to draw this shape without either lifting up your pen or re-visiting the same segment twice.

Tagged with:

Logic Gates Circuits in Cars

In this set of challenges you will design and test some logic gates circuits to control different automated features of a car including:

  • Automatic Headlights System
  • Interior Light System
  • Automatic Windscreen Wipers System
Automatic Headlights SystemInterior Light SystemAutomatic windscreen wipers systemDashboard Seatbelts Indicator

Automatic Headlights System


On most modern cars, the car can automatically turn the lights on and off when it detects it is driven at night time or when the driver is driving in rainy conditions. Nowadays most cars are equipped with the following sensors and switches to control the headlights:

  • A light sensor that detects when it is day time. A light sensor would be “on” (1) when there is light outside, but would automatically turn off (0) when it is dark outside (night time or driving through a tunnel).
  • A humidity sensor that detects when it is raining. A humidity sensor would be “on” (1) when it is raining outside, but automatically turns off (0) when is dry outside.
  • A engine switch to turn the engine on (1) or off (0).
  • A light switch to turn the lights on (1) or off (0).

Your task is a to design and test a logic gates circuit for the automatic headlights system that will apply the following rules:

  • The headlights are on if the engine is on and it is dark outside
  • The headlights are on if the engine is on and it is rainy outside
  • The headlights are on if the light switch is turned on (even when the engine is off)
  • If none of the above conditions are met, the headlights are turned off


Interior Light System


The interior light system (aka dome light) is used to lit up the inside of the car. The light is located on the ceiling of the car and can be manually turned on and off using a light switch located next to the light. Most cars are also equipped with sensors on each door. These sensors are on (1) when the door is open and off when the door is closed (0). Each door has its own sensor, including the driver door, the passenger door and the door to access the boot.

Your task is a to design and test a logic gates circuit for the interior light system that will apply the following rules:

  • The interior light is on if the light switch is on
  • The interior light is on if at least one of the door is open (on driver the driver side, passenger side or the door for the boot)
  • If none of the above conditions are met, the interior light is turned off


Automatic windscreen wipers system


The windscreen wipers system is used to automatically detect when the screen wipers should be turned on to improve the driving condition/visibility of the driver when it’s raining. They rely on a humidity sensor located on the windscreen that is on (1) when it detects water/rain. The windscreen wipers can also be turned on manually using a switch on the dashboard of the car.

Your task is a to design and test a logic gates circuit for the automatic windscreen wipers system that will apply the following rules:

  • The wipers are turned on if the engine is on and the humidity sensors detects rain water on the windscreen
  • The wipers are turned on if the engine is on and the driver has turned the wipers switch on
  • If none of the above conditions are met, the windscreen wipers are turned off


Dashboard Seatbelts Indicator


On the dashboard of this two-seater sports car, a light indicator is used to indicate if either the driver (D.) or the passenger (P.) (or both) have not locked their seatbelts.

The car uses weight/pressure sensors underneath each seat to detect if the seat is occupied (sensor on) or not (sensor off).
Each seatbelt has a locking mechanism with a sensor to detect if the seatbelt is on or off.

Design and test a logic gates circuit to control the seatbelt light indicator on the dashboard of the car.


unlock-access

Solution...

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

The Computer Shop – SQL Task

Working for an online computer shop, you have been asked to assist customers in finding the best computer deal to match their requirements and needs. Your shop has access to a large variety of laptop and desktop computers and hence you should be able to recommend a computer for every customer!

To make the right recommendation to your customers, you have been given access to an online database of all the computers available in stock. You can access and query this online database using our online SQL Editor:
Open in new windowAccess Online Database / SQL Editor

Your job is to:

  • Pick a customer and read through their requirements (see tabs below),
  • Write an SQL query to interrogate the database to identify the computer that meets their requirements,
  • Repeat this process for each customer.

Note that each one of your query should return one and only one computer that fully satisfies the customer’s demands.

Here are some examples of SQL queries based on the computers table:

SELECT * FROM computers WHERE type="Laptop" AND touchscreen=TRUE AND screenSize>=15;
SELECT * FROM computers WHERE type="Desktop" AND (HDD>1000 OR SSD>1000);
SELECT * FROM computers WHERE type="Laptop" AND RAM>=8 AND operatingSystem="Windows" AND (price>=300 AND price<=400);

Customer A:Customer B:Customer C:Customer D:Customer E:
Customer A is looking for a laptop computer to work on the train when commuting to work. They need a Windows laptop that is easy to carry around so would like a screen size of less than 16 inches. They would like the laptop to be touchscreen and would need an SSD drive of at least 512GB. They can spend up to £500 on this laptop.


SQL Query:

Recommended Computer:

Customer B is looking for a multimedia desktop computer to watch movies and stream music online. They would like a dual core or a quad core CPU with a clock speed of at least 2.5Ghz. They would like a HDD drive of at least 2TB (2000GB). They would like a large screen of at least 16 inches.


SQL Query:

Recommended Computer:

Customer C is a graphic designer who would like a desktop computer to use graphic editing and photo-editing software. They are looking for a MacOS desktop computer with a touchscreen of 17 inches. They would need an SSD drive of at least 256GB and a secondary hard drive to backup their work with at least 1TB (1000GB). They are happy to spend up to £600 on their computer.


SQL Query:

Recommended Computer:

Customer D is a student who needs a laptop to take notes during lectures. They have a small budget (up to £350) and would like a computer with at 6GB or 8GB of RAM, a screen size between 14 to 16 inches, a secondary drive either HDD or SSD of at least 512GB and the Windows operating system already installed.


SQL Query:

Recommended Computer:

Customer E is looking for a laptop to be used by his family around the house. They would prefer it to be a dual core laptop with at least 8GB of RAM. They would like a touchscreen of 15.6 inches and an SSD drive of either 256GB or 512GB.


SQL Query:

Recommended Computer:

Extension Tasks:

Warning: When completing the following extension tasks, note that, on this online system, Boolean values are actually stored as string containing the values “TRUE” and “FALSE”. This means that, in your queries, you will need to use “speechmarks” when storing the values “TRUE” and “FALSE” as string instead of Boolean values.

Price ListNew Computer in StockDiscount on Windows LaptopsDiscontinued Range
The shop manager would like you to produce a price list, only listing the name, type, operating system and price of all the Laptops in stock, in descending order of price (from the most expensive computer to the cheapest computer).

SQL Query:

A new computer has been delivered to the shop and needs to be added to the database. Run a query to add this computer to the computers table. The characteristics of this new computer are as follows:

Name Notebook NB-450
Type Laptop
Number of Cores 4 (Quad Core)
Clock Speed 3.5GHz
RAM 16GB
Hard Disk Drive 1TB
Solid State Drive 256GB
Screen Size 16
Touchscreen
Operating System MacOS
Price £550

Warning: In this computers table the ID field is not set as an AUTO_INCREMENT field. Which means that you will have to provide a value for the ID field within your INSERT query.

SQL Query:

The shop would like to apply a 20% discount on all Windows laptops. Use a query to update the reduce the price of all windows laptops by 20%.

SQL Query:

The shop has decided to stop selling computers from the “Smartpad” series and from the “Smartbook” series. Use a query to remove all the computers from the computers table where the name of the computer starts with either “Smartpad” or “Smartbook”.

SQL Query:

unlock-access

Solution...

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

The Bike Shop – SQL Task

As a newly appointed shop assistant for your local bike shop, you have been asked to help customers find their dream bike: a bike that meets all of their requirements. Your shop has access to a large variety of mountain bikes, road bikes and hybrid bikes and hence you should be able to find a bike for every customer!

Each of these bikes has its own specifications such as frame type (crossbar or low-edge frame), frame size (from XS to XL), wheels size, number of gears and so on.

To assist you in helping find the perfect bikes for your customers, you have been given access to an online database of all the bikes available in stock. You can access and query this online database using our online SQL Editor:
Open in new windowAccess Online Database / SQL Editor

Your job is to:

  • Pick a customer and read through their requirements (see tabs below),
  • Write an SQL query to interrogate the database to identify the bike that meets their requirements,
  • Repeat this process for each customer.

Note that each one of your query should return one and only one bike that fully satisfies the customer’s demands.

Here are some examples of SQL queries based on the bikes table:

SELECT * FROM bikes WHERE type="Mountain Bike" AND frame_size="L" AND electric=TRUE AND weight<10;
SELECT * FROM bikes WHERE type="Hybrid Bike" AND (frame_size="L" OR frame_size="XL") AND Hydraulic_Brakes=True;
SELECT * FROM bikes WHERE type="Road Bike" AND (price>=300 AND price<=500);

Customer A: Milly SpokeCustomer B: Tim SaddleCustomer C: Ayaz BearingCustomer D: Nikky HelmetCustomer E: Fred TyreCustomer F: Hamza ChainstayCustomer G: Joe CrossbarCustomer H: Lin DynamoCustomer I: Marek HubcapCustomer J: Anna Fork
Milly Spoke is looking for a hybrid bike to commute every day to her college. She would like a bike in the price range of £150 to £250. Ideally her bike should not be too heavy so she is looking at a bike of less than 15kg. Based on her height, she was recommended to consider an M frame size. She is not too fussy about technical characteristics such as number of gears or types of brakes!


SQL Query:

Recommended Bike:

Tim Saddle is a keen cyclist and would like to improve his performance when taking part in bike races. He is very specific in his requirements and is looking for a state of the art road bike with at least 27 gears, hydraulic brakes, disc brakes with a very light frame (<8 kg), 20 inches wheels and a large frame size (L). He has no limits on how much he can spend for this bike!


SQL Query:

Recommended Bike:

Ayaz Bearing is looking for an electric Mountain Bike to go mountain biking during the holidays. He would like a Crossbar frame with at least 21 gears. He is not too sure about the size so would like to find out if you have any bike in stock with either an L frame or XL frame.


SQL Query:

Recommended Bike:

Nikky Helmet is looking for either a mountain bike or hybrid bike to go through cycling paths in the forest. She is looking for an L frame, low-step frame and would like the bike to be fitted with hydraulic brakes.


SQL Query:

Recommended Bike:

Fred Tyre works in the city and is commuting to work everyday by bike. He is looking for a hybrid e-bike (electric). He needs an M frame with hydraulic disc brakes and has a budget of £500 to £700.


SQL Query:

Recommended Bike:

Hamza Chainstay is looking for a road bike, with a crossbar frame size M and wheels of 24, 26 or 27 inches. He would like the bike to weight no more than 12kg.


SQL Query:

Recommended Bike:

Joe Crossbar is looking for a mountain bike, with a crossbar frame size M or L. He would like hydraulic brakes but does not want the bike to be fitted with disc brakes. He is not interested in electric bikes.


SQL Query:

Recommended Bike:

Lin Dynamo is looking for an electric hybrid bike to commute to work every day. She would like a low-step frame but is not too sure of the size she needs. She would like to see if you have either an S or an M frame in stock. She would also like to spend less than £600 on her bike.


SQL Query:

Recommended Bike:

Marek Hubcap would like a road bike within a budget of £200 to £300 and at least 24 gears. He would like to have disc brakes and a light frame e.g. less than 10 kg.


SQL Query:

Recommended Bike:

Anna Fork would like a non-electric mountain bike, frame M with 21 or 24 gears, hydraulic and disc brakes. She has a budget of £300 to £400 for this bike.


SQL Query:

Recommended Bike:

Extension Tasks:

Warning: When completing the following extension tasks, note that, on this online system, Boolean values are actually stored as string containing the values “TRUE” and “FALSE”. This means that, in your queries, you will need to use “speechmarks” when storing the values “TRUE” and “FALSE” as string instead of Boolean values.

Price ListNew Bike in StockDiscount on Electric BikesDiscontinued Range
The shop manager would like you to produce a price list, only listing the name, type, frame size and price of all the Mountain bikes in stock, in descending order of price (from the most expensive bike to the cheapest bike).

SQL Query:

A new bike has been delivered to the shop and needs to be added to the database. Run a query to add this bike to the bikes table. The characteristics of this new bike are as follows:

Name Urban Explorer X-200
Type Hybrid
Frame Type Crossbar
Electric
Frame Size M
Wheel Size 24 inches
Gears 21
Weight 14.2Kg
Hydraulic Brakes
Disc Brakes
Price £650

Warning: In this “bikes” table the ID field is not set as an AUTO_INCREMENT field. Which means that you will have to provide a value for the ID field within your INSERT query.

SQL Query:

The shop would like to apply a 10% discount on all electric bikes. Use a query to update the reduce the price of all electric bikes by 10%.

SQL Query:

The shop has decided to stop selling bikes from the “Road Rider” series. Use a query to remove all the bikes from the bikes table where the name of the bike starts with “Road Rider”.

SQL Query:

unlock-access

Solution...

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

Drawing flags using Python Turtle

In this challenge we will use Python Turtle to draw a range of flags on screen.

First, you will need to investigate the code used to create three different flags:

France Sweden Czech Republic

To understand these code snippets you need to understand how to position the turtle (pen) on the screen using (x,y) coordinates. The following two templates will help you understand coordinates and be useful to help you design the algorithm to draw your own flags.

Python Code

Here is the code for our first three flags.

FranceSwedenCzech Republic

France

Sweden

Czech Republic

Your Turn

You can edit the trinkets above to recreate your own selection of flags. Here are some flags you could try to reproduce:

Chad

Luxembourg

Columbia

Iceland

Jamaica

Ghana

Turkey

Tanzania

Cuba

Tagged with:

Boolean Algebra using Logic Gates

Boolean Algebra is used to simplify long/complex Boolean expressions. It is needed to optimise the use of logics gates (and hence transistors) when making an electronic circuit. Considering that the CPU is made of billions of logic gates, it is essential to find the optimal logic gates circuits to solve a given problem.

There are a few Boolean Algebra rules that are used to simplify Boolean expressions. In this challenge, you will recreate some logic gates circuits to verify some of these rules.

You can also investigate these rules further by simplifying Boolean expressions on this challenge.

The first few circuits have already been completed and your task is to create more logic gates circuits to verify the following rules.

Double NegationComplement LawsAssociative LawsDistributive LawsAbsorptive LawsDe Morgan's Rules

Double Negation

¬ ¬A = A

Logic Gates Circuit


Click on the above circuit to test it online

Complement Laws

A ∧ ¬A = 0
A ∨ ¬A = 1

Logic Gates Circuit


Click on the above circuit to test it online

Click on the above circuit to test it online

Associative Laws

(A ∧ B) ∧ C = A ∧ (B ∧ C)
(A ∨ B) ∨ C = A ∨ (B ∨ C)

Logic Gates Circuit


Click on the above circuit to test it online

Click on the above circuit to test it online

Distributive Laws

A ∧ (B ∨ C) = (A ∧ B) ∨ (A ∧ C)
A ∨ (B ∧ C) = (A ∨ B) ∧ (A ∨ C)

Logic Gates Circuit


Click on this picture to create a logic gates circuit to verify these rules

Absorptive Laws

A ∧ (A ∨ B) = A
A ∨ (A ∧ B) = A

Logic Gates Circuit


Click on this picture to create a logic gates circuit to verify these rules

De Morgan’s Rules

¬(A ∨ B) = ¬A ∧ ¬B
¬(A ∧ B) = ¬A ∨ ¬B

Logic Gates Circuit


Click on this picture to create a logic gates circuit to verify these rules

Tagged with: