More results...

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

BBC micro:bit – Higher or Lower Game

For this challenge you will design and write a program to play against the BBC micro:bit.

The micro:bit will display a random number between 0 and 100. It will then ask the end-user whether they believe the next number will be higher or lower. The program will then generate the next number. If the user guessed right (e.g. the next number is higher or lower than the previous one) then the user scores one point. The game stops when the user guesses it wrong.

micro-bit-higher-or-lower-5

Learning Objectives

By completing this challenge you are going to use selection (IF statements) and comparison operators such as >= or <= to compare numbers. You will also use variables to store the value of random numbers, and to keep and increment a score as the game progresses.

Solution


Try to implement the game by yourself on the BBC micro:bit website:

micro-bit-logo

If you are stuck you can always use our solution below:

Step 1: Initialising the game
For our game we will need three variables:

  • number: the current number, randomly selected (between 0 and 100) being displayed on the LED screen,
  • nextNumber: the next number, also randomly selected between 0 and 100, but kept secret, the user will decide if they beieve it will be higher or lower than the current number,
  • score: the score will start at 0 and increment by 1 everytime the user has a good guess.

micro-bit-higher-or-lower-1

Step 2: Button A: Lower
When the user presses button A, they believe the next number will be lower than the current number. We can check if they are right or not.

If they are correct, we will display a smiley face, add 1 to their score and the nextNumber will become the current number whereas a new nextNumber is generated for the game to continue.

If they are wrong though, we will display a sad face as well as the value of the next number followed y the user score.micro-bit-higher-or-lower-2

Step 3: Button B: Higher
Step 3 is very similar to step 2 but this time we are checking whether the next number is higher than the current number.micro-bit-higher-or-lower-3

Extension Task:


That’s it you have a fully working game. We can tweak it slightly though. For instance how could we code the micro:bit so that when the user presses both the A and B buttons at the same time, the game restarts from the beginning – a new set of numbers is generated and the score is reset to zero?

Tagged with:

Archery Scoring Algorithm

archeryIn this challenge we will write a Python program to randomly shoot an arrow on a target. We will then use Pythagoras’ Theorem to calculate the distance between the arrow impact and the centre of the target. This distance will let us find out how many points to award to this shoot.

Scoring System


For our program we will be using the following scoring system:
archery-target

Pythagoras’ Theorem


The arrow will be issued (x,y) coordinates randomly. Our script will use these coordinates to calculate the distance of the arrow from the centre of the target (0,0).

archery-pythagoras

With this distance we can then decide how many points to be given using the following criteria:

  • Distance between 0 – 30 pixels (Yellow Ring) = 10 points
  • Distance between 31 – 60 pixels (Red Ring) = 5 points
  • Distance between 61 – 90 pixels (Blue Ring) = 3 points
  • Distance between 91 – 120 pixels (Black Ring) = 2 points
  • Distance between 121 – 150 pixels (White Ring) = 1 point
  • Distance above 150 pixels (Off Target) = 0 point

The code so far…


As you can see we have started the code for you below but still have work to do on line 44 to 54 to calculate the distance of the arrow from the centre of the target and calculate the score.

You challenge is to complete this code (line 44 to 54).

Extension Task


Can you let the computer shoot three arrows and display the total score of all three arrows, by adding the score of each arrow.
unlock-access

Solution...

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

BBC micro:bit – Roll the Dice

diceIn this challenge we will code our BBC micro:bit to create a “rolling dice” animation.

Our code will generate random numbers between 1 and 6 to create the animation and will include 8 frames. The last frame will be the number picked up by the dice. Here is a preview of the animation (on an infinite loop):

bbc-micro-bit-roll-the-dice

You can recreate this digital dice using the BBC micro:bit website.

Building the code, step by step:

Our “rolling the dice” animation will start when the user presses the A button on the micro-bit.
micro-bit-button-a

To create the animation we will then use a loop (repeat 8 times):
micro-bit-repeat

For each frame we will generate a random number between 1 and 6 using the following code:
micro-bit-random-number

We will then check the value of this random number using an IF block:
micro-bit-if

To display this number as it will appear on a dice we will use the “show leds” block:
micro-bit-show-leds

We will repeat the last two steps for all the 6 possible faces of the dice by adding ELSE IF blocks to our IF block:
micro-bit-else-if

Finally we will add a pause between each frame (between each iteration of the loop):
micro-bit-pause

Final Code


Here is the complete code for our dice:
micro-bit-die-code

Tagged with:

BBC micro:bit – Digital Compass

compass-iconIn this challenge we will use the compass sensor from the BBC micro:bit to create a digital compass.

Based on the compass heading (retrieved ftom the sensor), the micro:bit will display one of the four cardinal points:

  • N for North
  • E for East
  • S for South
  • W for West

For the micro:bit to decide which cardinal point to display it will have to look at the compass headings (angle given in degrees).

Check the following pictures to understand how angles and cardinal points work:
compass-full

cardinal-points

Heading (in degrees) Cardinal Point
Between 315° and 360°
OR
Between 0° and 45°
North
Between 45° and 135° East
Between 135° and 225° South
Between 225° and 315° West

Coding the compass


Access the micro-bit website:
micro-bit-logo

Use the Block Programming interface to add the following code (click on picture to zoom in):
digital-compass-bbc-microbit-javascript-code

Calibrating the compass


The first time you will run this program, the BBC micro:bit will ask you to “Draw a circle”.

This step is required to calibrate the compass. Without this step the readings would be inaccurate.

To draw the circle on the LED display simply rotate the device to move “the light” around the LED screen and display a circle as follows:

micro-bit-circle .

Tagged with:

BBC micro:bit – Magic 8 Ball

For this project we are going to code our micro:bit to act as a magic 8 ball:
magic-eight-ball

The user will think of a question such as:

  • Will it be snowing tomorrow?
  • Will I be a rock star one day?
  • Will my program work?

Then they will shake the micro-bit. The micro-bit will than randomly pick one of the following four answers:

  • No way!
  • Probably!
  • Unlikely!
  • Definitely!

Have a go at completing the task by yourself and if you get stuck or don’t know how to get started check our solution below.

Access BBC micro:bit website:
micro-bit-logo

Solution


Extension Task


Tweak the code to add more possible answers such as:

  • I don’t think so
  • Maybe
  • No doubt about it
  • Not sure
unlock-access

Solution...

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

BBC micro:bit – Ticketing System

Now-ServingYour challenge consists of programming your BBC micro:bit to use it as a ticketing system. Ticketing systems are used in a shop to control the order of the queue of customers:

  1. The queue should start at the value 0, and should be displayed on the micro:bit LED screen.
  2. When a shop assistant is available they press button A to call for a customer: The queue number displayed on the micro:bit should increase by 1.
  3. The shop assistant should be able to cancel their action by pressing button B: in this case the queue number should decrease by 1.
  4. To reset the ticketing system, the shop assistant should press both A and B buttons simultaneously. The queue number will be reset to 0.

queue

Solution


Tagged with:

What’s My Change?

my-change

Your Challenge


Using either Python or HTML + JavaScript, write a program that prompts the end-user to enter two values:

  • Value 1: Amount to be paid by the customer
  • Value 2: Amount received from the customer

The program should then find out how many banknotes or coins of different values should be returned.
what-s-my-change

Tip:


Your program will accept banknotes of £20, £10 and £5 and the following coins: £2, £1, 50p, 20p, 10p, 5p, 2p, 1p.

The following flowchart will help you get started:
what-s-my-change-flowchart

Extension:


Add some validation rules to ensure that the user has entered valid currency values. You should also check that the amount received from the customer is greater or equal to the amount to be paid.

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 Zoo Keeper’s Puzzles

Learning Objectives:


This set of puzzles will improve your Python skills when using If Statements (Selection)
You will focus on:

  • Using comparison operators: ==, !=, < , >, <=, >=
  • Using Boolean Operators: AND, OR, NOT

if statements use comparison operators to compare values and make a decision based on the comparison.

The main comparison operators are:

Comparison Operator Meaning Example
== is equal to 4==4
!= is different from 4!=7
< is lower than 3<5
> is greater than 9>2
<= is lower or equal to 3<=5
5<=5
>= is greater or equal to 9>=2
5>=5

When using comparison operators the result of a comparison is either true or false. For instance:

  • 3 < 5 returns true,
  • 3 > 5 returns false.

It is also possible to combine comparisons together using boolean operators such as and and or. This enables to check if more than one comparison are true (and operator) or whether at least one of the comparisons is true (or operator). e.g.

  • (3 < 5) and (3 < 7) returns true,
  • (3 == 5) or (3 < 9) returns true,
  • (3 == 5) and (3 < 9) returns false.

Starter Activity


Click on the picture below to check your knowledge of computing operators:
operators-dominoes


zoo-parrot

Rio the talkative parrot

Rio the parrot gets very talkative if:

  • It is early in the morning (between 5am and 10am).
  • It is sunny outside.

Complete the code below to tell the zoo keeper whether Rio will be quiet or noisy.


zoo-hippo

Gloria’s the Hippopotamus


When it is hot outside (above 24 Celsius degrees), Gloria the Hippopotamus loves sunbathing for 1 hour (=60 minutes) before having a mud bath to cool down.

Complete the code below to tell the zoo keeper where he can expect to find Gloria: either sunbathing outside on the grass or having a bath in the muddy puddle.


zoo-lions

Leo the sleepy lion!


Did you know that a Lion sleeps on average 18 hours a day! Once Leo is awake he gets very hungry.
Complete the program bellow to tell the Zoo keeper if Leo will be asleep, hungry or happy!

Leo will be:

  • Sleepy if his sleeping time has not reached 18 hours
  • Hungry if his sleeping time is over 18 hours and he has not been fed.
  • Happy if his sleeping time is over 18 hours and he has been fed.


zoo-penguins

Penguins’ sunbathing time

During the summer the penguins love to go for a swim when the water temperature in the pool is at least two degrees cooler than the outside temperature.

Complete the code below to tell the zoo keeper where he can expect to find the penguins: either sunbathing outside or swimming in the pool.


Tagged with:

Marathon Time Calculator

marathonIn this challenge you are going to write a Python script to help a marathoner predict the overall time they can complete a Marathon in (42km).

This estimation will be based on the runner’s pace which is the time they take to run 1km.

For instance:

  • Runner A runs 1km in 5:25 (5 minutes and 25 seconds)
  • Runner B, who is slower, runs 1km in 6:10 (6 minutes and 10 seconds)

Your Python script will:

  1. INPUT: Ask the runner for their pace (e.g. 5:25),
  2. PROCESS: Convert this input into a number of seconds: e.g. 5:25 = 5 * 60 + 25 = 325 seconds,
  3. PROCESS: Multiply this total by 42 as there are 42km in a Marathon,
  4. PROCESS: Convert this new total using the hh:mm:ss format. (e.g 3:47:30)
  5. OUTPUT: Display this new total on screen (using the hh:mm:ss format). (e.g 3:47:30)

Flowchart


The following flowchart describes the main steps of our algorithm based on the
Input > Process > Output model:
marathon-flowchart

Your Solution


Code your solution to this challenge:

Step by Step Solution:


The 5 steps described above are solved to help you complete this challenge step by step:

Step 1: Input the pace in the mm:ss format
This step is fairly straightforward. We will use an input() function to retrieve the user input and a variable called str_pace to store this input as a string.

str_pace = input("At what pace do you run a km? e.g. 5:21")
Step 2: Convert this input into a number of seconds
This step is more complex as we need to extract the left and right side of the input. In python we can split a string using a character as a separator, in this case the colon (:). We will also use the int() function to convert (cast) a string into an integer.

split_pace = str_pace.split(":")
minutes = int(split_pace[0])
seconds = int(split_pace[1])
pace = minutes * 60 + seconds
Step 3: Multiply this total by 42 as there are 42km in a Marathon
totalTime = pace * 42
Step 4: Convert this new total using the hh:mm:ss format
The totalTime tells us how many seconds will the runner complete the Marathon in. Considering that there are 3600 seconds in one hour and 60 seconds in one minute we can use the DIV (//) (whole division) and MOD (%) (remainder operators to convert our total time in the required format.

hours = totalTime // 3600
timeLeft = totalTime % 3600

minutes = timeLeft // 60
seconds = timeLeft % 60
Step 5: Display this new total on screen
print("You should complete a marathon in " + str(hours) + ":" + str(minutes) + ":" + str(seconds) +".")
Note: To be 100% accurate you should time the pace by 42.195 instead of 42 as the exact length of a Marathon is 42km and 195 meters.

Testing


Now that you have implemented your solution you may want to test it by completing the following test plan: (this test plan is based on a length of 42km)

Test # Input Values Expected Output Actual Output
#1 Pace: 5:25 3:47:30
#2 Pace: 6:10 4:19:00
#3 Pace: 4:00 2:48:00
#4 Pace: 2:55 (Close to World Record) 2:02:30
unlock-access

Solution...

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

BBC micro:bit – Voting System

yes-noIn this challenge you will create a voting system using the BBC micro:bit. You can complete the code online on the BBC micro:bit website.

The idea is to use the BBC micro:bit in the classroom to conduct a survey by asking a yes/no question such as:

  • Should our school change our school uniform?
  • Should we have more computer science lessons on our timetable?
  • Should we have more healthy eating options on our school menu?

Then you will pass the micro:bit around so that each pupil can vote using the A and B buttons of the microbit:

  • A: Vote Yes
  • B: Vote No

Using two variables called voteA and voteB the BBC micro:bit will keep track how many times the A button is pressed and how many times the B button is pressed.

After each vote the BBC micro:bit will display a tick for two seconds to confirm the vote.

bbcmicrobit-vote-preview

To finish the vote the user will press both A and B buttons at the same time. In this case the BBC micro:bit will display how many A votes have been recorded (variable voteA), how many B votes have been recorded (variable voteB) and then use an If Statement to decide if option A (Yes) Or B (No) wins. To do so it will use comparison operators such as > or <. (if voteA > voteB then A wins)

Building the voting system step by step:


Step 1: Initialising the BBC micro:bit
bbcmicrobit-vote-1Let’s initialise our voteA and voteB variables that will be used to count how many tumes the A nad B buttons have been pressed.
Step 2: When button A is pressed
When button A is pressed we need to increment A by 1. Then we can display a tick to confirm the vote was recorded.
bbcmicrobit-vote-A
We then display a question mark to inform the user the BBC micro:bit is ready to accept another vote.
Step 3: When button B is pressed
Very similar to the previous code, this time to record the B vote.
bbcmicrobit-vote-B
Step 4: When A+B are pressed simultaneously
The user pressed both A and B at the same time to end the vote. In this case the BBC micro:bit will display how many A votes were recorded, how many B votes were recorded and decide if A or B wins or if it is a draw.
bbcmicrobit-vote-AB

Complete and Test your Voting System


Complete the system using the code provided above.

Test the game using the online emulator. Then when you are confident if fully works, download your code to the BBC micro:bit and conduct your first survey with your classmates. Make sure you ask for a question with only two possible answers (e.g. Yes/No question).

Tagged with: