-
Introduction
-
Introducing Scratch
- Scratch Ideas
-
Activities Overview
- Scratch Activities Project
-
Algorithms and Programs
-
Write an Algorithm
-
- Sequences
- Loops
- Data: Variables
- User Input
- Conditionals
- Conclusion
Dealing with Negative Numbers
Dealing with Negative Numbers
We will use Conditionals to deal with what is called invalid input.
Invalid input is user input that is nonsensical or that causes the program to do unexpected things.
In this example, we will look at a specific invalid input: answering the Cat's question about how many times to dance with a negative number.
What happens if we answer the Cat's question (How many times would you like me to dance?) with a negative number (for example: -2) or 0?
If you try it and see what happens, you may notice that the Cat doesn't do anything. This is because the Cat can't dance a negative number or 0 times.
We will add Conditionals to our Dancing Cat program, so that the Cat tells the user that they need to input a positive number.
Look at the Scratch blocks below, where an if then else block, a say block and two join blocks have been added to the program:
Change your Dancing Cat program to match the above.
Once you have made these changes, answer the Cat's question with a number like 0 or -2.
The Cat should say that it cannot dance that many times and refuse to dance.
If you enter a number larger than 0, such as 4, the Cat will follow the instructions (the dance steps) that are inside the else block.
Views | |
---|---|
1 | Total Views |
1 | Members Views |
0 | Public Views |
Share by mail
Please login to share this webpage by email.