690 likes | 943 Views
Branching and Looping Examples, cont’d. Remember the generic triple jump world…. Let’s change this to be interactive. User input functions in Alice. ask user for a number ask user for yes or no ask user for a string. Start Alice and open genericTripleJump Look at world. my first method
E N D
User input functions in Alice • ask user for a number • ask user for yes or no • ask user for a string
Start Alice and open genericTripleJump • Look at world. my first method • Note that the statements there form a linear sequence. • We want to modify this algorithm.
The first step is to add an If/Else instruction to the method. • Drag a copy of the If/Else tile from the bottom of the editor area. • Drop it into the method just below the three jump instructions.
Adding If/Else instructions • When adding an If/Else, you will be prompted to choose between true “and” “false” as an initial value for the condition. • This value is merely a placeholder. • We will always replace this value with a Boolean expression of a condition we want to test.
Replace the placeholder condition in the If/Else instruction with a function. • We need the function that will allow us to ask the user a yes/no question.
We want Alice to jump only if the user answers yes to our query. • Need to move the instruction that causes Alice to jump to the If clause of our If/Else instruction.
genericTripleJumpWhile.a2w • genericTripleJumpLoop.a2w
A Sentinel Sailing Loop • Sailboat will turn to face the object • Will sail one meter forward • This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
A Sentinel Sailing Loop • Let’s create a sail to method that will work with any object. • It should accept the target object as an input parameter. • This will be similar to the way the primitive move method accepts direction and amount
A Sentinel Sailing Loop • Sailboat will turn to face the object • Will sail one meter forward • This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }
A Sentinel Sailing Loop • Sailboat will turn to face the object • Will sail one meter forward • This process continues until the sailboat is within 5 meters of the object While (NOT (the sailboat is within 5 meters of the [object])) { turn to face [object] move forward 1 meter }