Detect parity error in any number of rows (after each row is entered)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program which asks the user to enter any number of rows and checks if there is a parity error in any of the rows after entering each row.

Testing examples:

Your program should display the outputs shown in these panels for the given inputs provided:

Input Output
WWW Row 1 is ok!
WBW There is a parity error in row 2!
WBB Row 3 is ok!

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked
ask [How many rows would you like to enter?] and wait

ask (join (join (join [Please enter ] (number of rows)) [ cards for row ]) (row number)) and wait
set [black cards total v] to [0]

set [row number v] to [1]

set [index v] to [1]

set [number of rows v] to (answer)

set [row v] to (answer)

change [black cards total v] by (1)

change [index v] by (1)

change [row number v] by (1)

set [black cards total v] to [0]
say (join [There is a parity error is row ] (row number)) for (2) secs

say (join (join [Row ] (row number)) [ is OK!]) for (2) secs
if <(letter (index) of (row)) = [B]> then
end

repeat (number of rows)
end

repeat (length of (row))
end

if <((black cards total) mod (2)) = [0]> then
else
end
Hints
  • Make a variable called “number of rows” and set its value to the input entered by the end user. Make a variable called “black cards total” and set its value to 0. Also make a string variable called “row” and set its value to the input entered by the end user. Check each letter of the string and if it’s ‘B’ add 1 to the “black cards total”. If the value of the “black cards total” is even (after checking every card in the row), display a message that the parity row is ok. If the value of the “black cards total” is odd, display a message that the row has a parity error. Repeat this “number of rows” times (for each row).

  • You can access a letter at the specified position in a string by using the letter (1) of [world] block under “Operators”. For example: letter (1) of [world] //w

  • In this challenge you need to access all the letters in user’s input (each row of the parity trick) and check to see how many of them are equal to B (black). Store the total number of black squares in a variable called “black cards total”.

  • You can find how many letters a string has by using the length of [world] block unders “Operators”.

  • To find out if a number is even or odd, use the () mod () block (under "Operators") to find the remainder after dividing that number by two. If the remainder is zero the number is even. For example: (37) mod (10) //7

Show Scratch solution