Skip to main content

Featured

Space Explorers Book 7: Answers and Explanations

Memunic Space Explorers › Book 7: Memoon and the Search for Another Home › Answers and Explanations Answers and Explanations Habitable means life could live there; inhabited means life does live there. A world can be habitable and empty, as an unoccupied flat has a roof and water but nobody in it. The Moon has almost no air, so water would boil or freeze, and its gravity is only about one-sixth of Earth’s, too weak to keep air. Accept also: no greenhouse blanket, so it swings between very hot and very cold. Venus. It matches Earth in size and roughly in mass, but its thick carbon dioxide air traps heat, making it hot and crushing. “Twin” is only true for size and weight. A dip of 1 per cent means (planet width divided by star width) squared is 0.01, so the ratio is 0.1: the planet is about a tenth as wide as the star. Repeats at the same gap show the planet is going round the star in 12 days, and rule out a one-time event such as a passing object. A planet shines only by reflected l...

Byte by Byte Book 4: Chapter 10 – Conditions and Sensing

Byte by Byte › Book 4: Office Apps and First Programs › Chapter 10: Conditions and Sensing

Chapter 10: Conditions and Sensing

You will learn: if-then and if-then-else blocks in Scratch, Boolean values (true and false), sensing blocks, comparison operators (<, >, =), combining conditions with and, or and not, and putting conditions inside a forever loop so a program keeps checking.

🌟 The Adventure

The Queen had one last wish for the show. "Let the audience join in," she said. "A game at the end: Beautine must catch falling festival stars before they reach the ground."

Memoon made the stars fall with a loop, and Beautine moved with the arrow keys. But the stars fell straight through Beautine as if she were a ghost. Nothing happened when they touched. Stars that reached the ground just sat there in a pile. And Beautine could run right off the side of the stage and disappear.

"The program doesn't notice anything," said Memoon. "It just follows the steps. It needs to look around and make decisions."

Beautine clutched her tail. "You mean it needs to ask questions? Like Am I touching a star? Yes or no?"

"Exactly," said Memoon. "Questions with yes-or-no answers. If yes, do one thing. Else, do another."

Illustration from Byte by Byte Book 4: Chapter 10 – Conditions and Sensing

💡 Learn With Memoon

Conditions and Boolean values. A condition is a question whose answer is either true or false. True/false values are called Boolean values, after the mathematician George Boole. In Scratch, conditions are the hexagonal blocks, and they fit only into hexagonal slots.

If-then and if-then-else. You met if, then, else unplugged in Book 3. In Scratch they are orange C blocks in Control:

BlockWhat it does
if ⟨condition⟩ thenRuns the blocks inside only if the condition is true; otherwise skips them
if ⟨condition⟩ then … else …Runs the first set if true, the second set if false. Exactly one set always runs

An if block checks once, at the moment the script reaches it. To keep checking all the time, put it inside a forever loop. This pattern, forever + if, is at the heart of almost every game.

when green flag clicked
forever
    if ⟨touching Star?⟩ then
        play sound Pop
        say Got one! for 0.5 seconds

Sensing blocks. The light-blue Sensing category lets a sprite find out about the world:

Sensing blockReports
touching (mouse-pointer / edge / sprite)?True if touching it
touching color ( )?True if touching that colour
key (space) pressed?True while that key is held down
mouse down?True while the mouse button is pressed
distance to ( )A number: how far away something is
ask ( ) and wait / answerAsks the user a question and stores what they type
timer / reset timerSeconds since the program started or the timer was reset

Comparison operators. In the green Operators category, three blocks compare two values and give true or false:

OperatorMeansExampleResult
<less than3 < 7true
>greater than3 > 7false
=equal to5 = 5true

For example, if ⟨(y position) < -150⟩ then asks "has the star fallen below the bottom area?" and if ⟨(timer) > 30⟩ then asks "have 30 seconds passed?".

Combining conditions: and, or, not.

OperatorTrue whenExample
andBoth conditions are true⟨key right arrow pressed?⟩ and ⟨(x position) < 220⟩
orAt least one is true⟨touching edge?⟩ or ⟨touching Rock?⟩
notThe condition is falsenot ⟨touching Star?⟩

A truth table shows every possibility:

ABA and BA or B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Asking the user. ask (What is your name?) and wait shows a box for typing. Whatever the user types is stored in the round answer block. Combine it with join (Operators) to reply: say (join (Hello, ) (answer)). Combine it with if-else to make a quiz:

ask (How many sides does a hexagon have?) and wait
if ⟨(answer) = 6⟩ then
    say Correct! for 2 seconds
else
    say Not quite. It has 6. for 2 seconds

Random numbers. pick random (1) to (10) (Operators) gives a different whole number each time. It makes games less predictable, for example go to x: (pick random -220 to 220) y: 180 drops a star from a random place along the top.

Illustration from Byte by Byte Book 4: Chapter 10 – Conditions and Sensing

✍️ Try It Yourself

Activity 1: Catch the star. You will build the Queen's game.

  1. Add a catcher sprite (try Squirrel if there is one, or any animal) and a Star sprite. Choose a night backdrop.
  2. Catcher: when green flag clicked → go to x: 0 y: -140 → forever containing two if blocks:
  • if ⟨⟨key right arrow pressed?⟩ and ⟨(x position) < 220⟩⟩ then change x by 10
  • if ⟨⟨key left arrow pressed?⟩ and ⟨(x position) > -220⟩⟩ then change x by -10
  1. Star: when green flag clicked → go to x: (pick random -220 to 220) y: 180 → forever containing:
  • change y by -5
  • if ⟨touching (catcher)?⟩ then → start sound Collect → go to x: (pick random -220 to 220) y: 180
  • if ⟨(y position) < -170⟩ then → go to x: (pick random -220 to 220) y: 180
  1. Play. Does the catcher stop at the edges? Why does the and matter?
  2. Change -5 to -8. What happens to the difficulty?

Activity 2: Quiz master. Write a three-question quiz with ask and wait and if-then-else. Include one maths question, one question from this book (for example What does BCC stand for?), and one about you. (Scratch compares answers without caring about capital letters.)

Activity 3: Timed challenge. On the stage, add when green flag clicked → reset timer → wait until ⟨(timer) > 30⟩ → stop all. Now your star game lasts 30 seconds.

Activity 4: Truth detectives. Without the computer, decide true or false: (a) 8 > 3 and 2 > 5; (b) 8 > 3 or 2 > 5; (c) not (4 = 4); (d) (10 < 20) and (20 < 30).

Unplugged: Simon Says, with logic. The leader gives commands with conditions: If you are wearing blue AND you have a pet, stand up. If you like mango OR you like chikoo, clap. If you are NOT holding anything, wave. Everyone checks their own conditions.

🎉 Memoon Solves It

Memoon gave the program its questions. The star asked, again and again, Am I touching Beautine? If true, it played a sparkly sound and jumped back to a random spot at the top. Am I below the floor? If true, it started again from the top. Beautine asked Is the right arrow pressed AND am I not at the edge yet? before every step, so she never ran off the stage.

The audience tried it at the dress rehearsal. Pip the sparrow caught nineteen stars in thirty seconds and refused to give the keyboard back.

Beautine fanned herself with her tail. "I make decisions now," she announced. "If someone admires me, then I bow. Else I wait for them to admire me."

❓ Check Your Knowledge

  1. What is a Boolean value? What shape are Boolean blocks in Scratch?
  2. What is the difference between if-then and if-then-else?
  3. Why do games usually put if blocks inside a forever loop?
  4. Name three sensing blocks and what they report.
  5. Is 7 < 4 true or false? Is 5 = 5?
  6. When is A and B true? When is A or B true?
  7. What does not ⟨touching edge?⟩ report when the sprite is touching the edge?
  8. Where is the user's typed reply stored after ask and wait?
  9. What does pick random 1 to 6 do? What real object could it imitate?
  10. Challenge: Plan a script that says Too hot! if a number is greater than 35, Too cold! if it is less than 15, and Just right! otherwise. (Hint: an if-else inside another if-else.)

📚 Word Bank

  • Condition: a question with a true or false answer.
  • Boolean: a value that is either true or false.
  • If-then / if-then-else: blocks that run code only when a condition is true / choose between two sets of code.
  • Sensing: blocks that let a sprite detect touching, keys, the mouse, time and answers.
  • Comparison operator: <, > or =, used to compare two values.
  • And / or / not: operators that combine or reverse conditions.
  • Truth table: a table showing the result of a logic operator for every combination.
  • Answer: the block that stores what the user typed.
  • Random number: a number chosen by chance within a range.

Keep learning with Byte by Byte

Explore Book 4: Office Apps and First Programs. Find the ebook, purchase details and teaching slides on MemuniCity.

Watch this chapter on YouTube

Comments

Popular posts from this blog

CA-7 Job Status and Failure Reason Codes

Here are the various status codes that You may see for jobs in the Queue or Logs in CA-7. R-C0002 R-C0003 R-C0099 R-C0255 R-JCLER A-U0002 A-U0004 A-U0015 A-U0043 A-U4095 A-SOC7 A-U4000 Reason Code/Job Status Code explanation: The indication of the job's current disposition. Values and their meanings are: A-Snnnn-- The job has failed with a system abend. A-Unnnn --The job has failed with a user abend. C-Cnnnn --The job has completed successfully and is going through completion processing. E-ARFATT-- An error occurred during the attempt to attach ARF definitions. This can result if the ARFSET specified is not defined. A diagnostic message may have been written to the CA-7 log, the OS console, or to the trailer queue for the job (in which case it can be displayed using LQ,JOB=x,LIST=ALL). E-ARFERR ---The job encountered an error in the ARF processing. Do a LARFQ command for the job to get more specific information. E-SEGERR --The job has been requeued...

Whistling Winds and Waterfalls: 4 Days of Magic in Shillong

Nestled in the clouds, where the scent of pine trees mingles with the rhythm of waterfalls and the tunes of local guitar-strumming youth—Shillong is more than a place. It’s a vibe. If you’ve got 3 nights and 4 days, you’ve got just enough time to fall in love. Here’s how to make the most of it—food, fun, fog and fantastic views included. Day 1: Shillong, We’re Coming for You! You land in Guwahati, barely out of the airport when the hills start calling. You hop into your cab, Spotify on, windows down, and the three-hour ride to Shillong begins. Just when you think it can’t get better, you stop at Umiam Lake —a lake so calm, it looks like someone ironed the water. By evening, you’re sipping hot chai in Laitumkhrah, maybe nibbling on spicy momos or pork jadoh from a roadside stall. You’re checked into your charming hotel ( Tripura Castle if you fancy old-world royalty, Ri Kynjai if you’re into lakeside poetry). The breeze whispers secrets of waterfalls and pine forests. Sleep well, drea...

Installing and Configuring Hadoop on Ubuntu as a Pseudo Distributed Single Node Cluster

Briefly what is psuedo-distributed single node Hadoop cluster: It is a Hadoop distributed cluster having Single Node. All the five daemons (NN,DN,JT,TT,SNN) are running as individual JVM (java process) instances. We will get all distributed features of Hadoop. This mode is used to develop, test, and staging (before production) Map Reduce programs quickly. It writes all the system, daemon and user logs to different files on the cluster To install Hadoop in this mode, we have to modify the hadoop-env.sh, core-site.xml, hdfs-site.xml, mapred-site.xml. hadoop-env.sh contains all the Environment variables which are required for running Hadoop. All the default values are fine, Only we have to modify the JAVA_HOME=“The location of Java Home Directory” Core-site.xml contains all the cluster specific information like io, temp directory, sorting, file system uri etc. Hdfs-site.xml contains all the parameters which are specific to HDFS like name node directory, data node directory...