1 / 24

Variables

Learn about variables and iteration in computer programming by creating a Scratch game that rewards points for choosing healthy food and deducts points for choosing unhealthy food.

meghanlove
Download Presentation

Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables Exploring Computer Science Lesson 4-8

  2. Objectives The students will be able to: • Explain the concept of variables. • Create examples of variables. • Explain the concept of iteration. • Create examples of iteration.

  3. Discussion What does the word variable mean in both mathematical and English terms?

  4. Variables • Look at this example: x + 3 = 5 • What is the variable? • What is the value of x? • Look at this example: 2x= 12 • What is the variable? • What is the value of x? • Why isn’t “x” the same in both cases?

  5. Variables in Programming • A variable is a name that represents a value that can be changed.

  6. Open Scratch! • You are going to build the beginning of a Scratch game. • The program will reward you with points when you choose a good food and take points away when you choose a bad food.

  7. Add Text and Sprites • Create a solid color background. • Add the text “Click on a food to eat it” to the background. • Add sprites for bananas and cheesy-puffs.

  8. Adding a Variable • Click on “Variables”. • Click on “Make a Variable”. • Name the variable “Good Nutrition Points” and set it for all sprites.

  9. Adding Nutrition Points • I want to add a point when the Banana sprite is clicked. • With the Banana sprite selected, add this block: • Adding 1 to a variable is called iteration.

  10. Subtracting Nutrition Points • I want to subtract a point when the Cheesy-puffs sprite is clicked. • With the Cheesy-puffs sprite selected, add this block:

  11. Initializing the Variable • We want the Nutrition Points to be set to zero when the game starts. • Add this block to any sprite:

  12. Customize Your Program! • Add a food that is worth 2 points when clicked. • Add a food that is -3 points when clicked. • Show me your program when you’re done. • For extra credit, separate your 1-scene program into 3 scenes with different backgrounds: title, instructions and game itself.

  13. The Good Nutrition 1 Program Rubric

  14. Conditionals Exploring Computer Science Lesson 4-9

  15. Objectives The students will be able to: • Explain the concept of conditionals. • Enhance a variable program with conditionals.

  16. Discussion • How would you finish a knock-knock joke when the third line begins with “If”? • What are some ways we use the word “if” in English?

  17. If Statements in Computing • If you have started to think about building animations like simulations and video games, then to build those more advanced programs, you will need to write code that involves decisions: If (some condition), then (do this).

  18. Examples • When you exit Microsoft Word, it asks you if you want to save your file. • If you click “Yes”, the file is saved. • If you click “No”, your changes are discarded. • In a car-race simulation, the driver steers the car around curves and past mile-markers. • If the car stays on the road, the score increases. • If the car goes off the road into the stands, the car crashes. • If the driver gets the car over the finish-line, the time is posted and the driver wins!

  19. Scratch “if” Block • The Scratch “if” block looks like this: • Note that only a hexagon shaped block will fit.

  20. Example • What does this program do?

  21. Expand the Program Currently, it only does the first condition. Your task is to finish the program so that the cat will tell you the rest: • If you are older than 2, “You don't need diapers." • If you are older than 15, “You can drive." • If you are older than 16, “You can see an R-rated movie." • If you are older than 17, “You can vote." • If you are older than 20, “You can gamble." • If you are older than 24, “You can rent a car." • If you are older than 49, “You can retire.“ • Finally add: If the age is less than 3, "Sorry. You are not old enough for anything yet!"

  22. Age Program Rubric

  23. Enhance Your Nutrition Program Enhance your Nutrition program by: • Adding a message about being nutritious if the number of points becomes greater than 9. • Adding a message about eating healthier food if the number of points becomes less than -4. • A sprite can say these messages or you can use broadcast to change the sprites/stage to convey the messages. • For extra credit, use both broadcast AND a sprite to convey the messages.

  24. The Good Nutrition 2 Program Rubric

More Related