Coding Ireland STEM Report 2024 Have Your Say
Mathematics Scratch
Expert
45 mins
155 points
What you need:
  • Computer/laptop

Around the World part 2

In this project we will do some math to calculate how long it would take to walk, drive or fly all the way around the world! This project comes in 2 parts, this part 2.

1 - Complete part 1 of this project

In a separate lesson you should have completed part 1 of this project. Open your project in Scratch and continue to the next step in this lesson.

2 - Create a 'round decimals (number)' custom block

As our formulas are using division, we may end up with answers that have many decimal places. For example:

40,075 / 1,086 = 36.90147329650092

To make things easier to read we will round these numbers to 2 decimal places, so the answer to the above sum would appear as 36.90.

We will be rounding our answers several times so to makes things easier for us we will create a custom block that has all the code for rounding a number to 2 decimal places.

In the My Blocks toolbox, create a new block called 'round decimals' and give it a number input called 'number'.

Once you've created the block give it the following code. This does the following:

  1. Multiply the number by 100 (e.g. 36.90147329650092 x 100 = 3,690.147329650092)
  2. Round the amount to the nearest whole number (e.g. 3,690)
  3. Divide the amount by 100 (e.g. 3,690 / 100 = 36.90)

define round decimals (number) set [amount v] to ((number) * (100)) set [amount v] to (round (amount)) set [amount v] to ((amount) / (100))



3 - Create a 'calculate (type) (speed)' custom block

We will also create a custom block that performs our calculations to work out the time in hours and in days and says the answers on the screen.

In the My Blocks toolbox, create a new block called 'calculate' with a text input called 'type' and a number input called 'speed'.

Once you've created the block give it the following code.

define calculate (type) (speed) say (join (join (type) [ travel at ]) (join (speed) [ kilometers per hour ...])) for (3) seconds round decimals ((earth) / (speed)) say (join [So it would take ] (join (amount) [ hours to go all the way around!])) for (3) seconds round decimals (((earth) / (speed)) / (24)) say (join [That's ] (join (amount) [ days!])) for (3) seconds define round decimals (number)



4 - Receive the messages and calculate

Our last step is to add some code to receive the messages and run the calculations. Depending on which message we receive we will use the calculate block and pass in the name and the speed that it travels.

Add the following code to the earth sprite.

when I receive [person v] calculate [People] (5) // we walk at 5 km per hour when I receive [car v] calculate [Cars] (100) // 100 km per hour for cars when I receive [plane v] calculate [Planes] (5) // 1,086 km per hour for planes when I receive [space station v] calculate [The international space station] (277724) // The international space station travels at 277,724 km per hour! define calculate (type) (speed)


5 - Test it out!

That's all the code for the project! Test it out by clicking the green flag and then clicking one of the sprites, it should calculate the time for the hours and the days while the object circles around the earth!


Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more