Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Computing Science
Scratch Programming
2
Worked
Example
3
3
Worked Example 3: Greetings
Read the description below of the problem we are going to solve.
Problem Description
A program is required that will ask the user to enter their first name.
It will then display a message to say Hi there which includes the name entered.
Design
The first step is to design the steps that the program must follow. We can do this with
a flow chart.
Step 1 The program starts. Step 2 The program must get the user’s first name. This is called an INPUT. Step 3 The program must display a message containing the user’s name. This is called an OUTPUT. Step 4 The program ends.
4
Converting Design to Code
We can now use the design we created to write the Scratch code needed to make
the program work.
You can see below how the flow chart shapes can be converted into the correct
Scratch blocks.
Flow Chart (Design) Scratch (Code)
Task 3a
Watch the Worked Example 3.mp4 video.
Follow the instructions on the next 3 pages to:
• Write the Scratch code
• Test your program
5
Writing the Code: Step-by-Step Guide
Step 1
Click Find Drag it into the middle area
Step 2
Click Find Drag it into the middle area and connect it to the green flag block.
Step 3
Click
Click Make a Variable.
Type the word firstname into the box. Click OK.
firstname
6
Step 4
Click Find Drag it into the middle area and connect it to the ask block.
Step 5
Click Find Drag it into the number box inside the orange variable block.
Step 6
Click Find Drag it into the middle area and connect it to the set block.
Step 7
Click Find Drag it into the text box inside the purple say block.
7
Step 8
Click Find Drag it into the right text box inside the green join block. and Type Hi there into the left text box in the join block
Testing the Code
1. Run the program by clicking on the green flag at the top
on the stage area.
2. Type a name into the box.
3. Check that Scratch says Hi there and adds the name you
typed.
4. Try running the program again and use a different name.
Jenna
8
Worked
Example
4
9
Worked Example 4: Adding
Read the description below of the problem we are going to solve.
Problem Description
A program is required that will ask the user to enter two numbers.
It will then add the numbers together and display a message containing the total.
Design
The first step is to design the steps that the program must follow. We can do this with
a flow chart.
Step 1 The program starts. Step 2 The program must get the user’s first number INPUT. Step 3 The program must get the user’s second number INPUT. Step 4 Add the two numbers together to make the total. Step 5 The program must display an OUTPUT message containing the total. Step 6 The program ends
10
Converting Design to Code
We can now use the design we created to write the Scratch code needed to make
the program work.
You can see below how the flow chart shapes can be converted into the correct
Scratch blocks.
Flow Chart (Design) Scratch (Code)
Task 4a
• Watch the Worked Example 4.mp4 video.
• Put the code together in the order shown above connecting all the blocks
together. (You can follow the instructions on the next 5 pages to help if you need to)
THEN
• Test your program (see page 16)
11
Writing the Code: Step by Step Guide
Step 1
Click Find Drag it into the middle area
Step 2
Click Find Connect it to your green flag block. Change the message to read, “Enter your first number”
Step 3
Click
Click Make a Variable.
Type the word firstnum into the box. Click OK.
firstnum
12
Step 4
Click Find Drag it into the middle area and connect it to the ask block.
Step 5
Click Find Drag it into the number box inside the firstnum variable block.
Step 6
Click Find Connect it to your green flag block. Change the message to read, “Enter your second number”
Step 7
Click Click Make a Variable.
Type the word secondnum into the box.
Click OK.
secondnum
13
Step 8
Click Find Drag it into the middle area, connect it to the ask block. **IMPORTANT** Use the drop-down to change it to secondnum.
Step 9
Click Find Drag it into the number box inside the secondnum variable block.
Step 10
Click
Click Make a Variable.
Type the word total into the box.
Click OK.
total
14
Step 11
Click Find Drag it into the middle area, connect it to the set block. **IMPORTANT** Use the drop-down to change it to total.
Step 12
Click Find Drag it into the text box inside the SET TOTAL block.
Step 13
Click Find and Drag them into the left and right text boxes in the green + block.
Step 14
Click Find Drag it into the middle area and connect it to the set block.
15
Step 15
Click Find Drag it into the text box inside the purple say block.
Step 16
Click Find Drag it into the right text box inside the green join block. and Type “The total is” into the left text box in the join block.
16
Testing the Code
1. Run the program by clicking on the green flag at the top
on the stage area.
2. Type your first number into the box.
3. Type your second number into the box.
4. Check that Scratch says Your total is and gives you the
correct total.
5. Try running the program again and use different numbers.
17
Worked
Example
5
18
Worked Example 5
Read the description below of the problem we are going to solve.
Problem Description
A program is required to ask the user to enter their name followed by their test scores in
Maths, English and Computing.
It will then calculate the average score. Finally, a message should be displayed with the
user’s name and average score.
19
Design
The first step is to design the steps that the program must follow. We can do this with
a flow chart.
Step 1 The program starts. Step 2 The program must get the user’s name INPUT. Step 3 The program must get the user’s Maths score INPUT. Step 4 The program must get the user’s English score INPUT. Step 5 The program must get the user’s Computing score INPUT. Step 6 The program calculates the total Step 7 The program calculates the average Step 8 The program must display an OUTPUT message containing the name and total. Step 9 The program ends
20
Converting Design to Code
We can now use the design we created to write the Scratch code needed to make
the program work.
Flow Chart (Design) Scratch (Code)
Task 5a
Watch the Worked Example 5.mp4 video.
Put the code together in the order shown above (connect all the blocks together)
21
Testing the Code
1. Run the program by clicking on the green flag at
the top on the stage area.
2. Type your name into the box.
3. Type your Maths score into the box.
4. Type your English score into the box
5. Type your Computing score into the box
6. Check that your average score is correct. It should be the total of your 3 scores
divided by 3.