Detect parity error in 6 rows (after each row is entered)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter 6 rows of black and white cards (6 cards in each row) 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 this table for the given inputs provided:

Input Output
WWWWWW Row 1 is ok!
BBWWBB Row 2 is ok!
BBBWWW There is a parity error in row 3!
BBBBBB Row 4 is ok!
WBBWBB Row 5 is ok!
WBWBWB There is a parity error in row 6!

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 (join [Please enter 6 black or white cards for row ] (row number)) and wait
repeat (6)
end

repeat (6)
end

if <(letter (index) of (row)) = [B]> then
end

if <((black cards total) mod (2)) = [0]> then
else
end
say (join (join [Row ] (row number)) [ is OK!]) for (2) secs

say (join [There is a parity error in row ] (row number)) for (2) secs
set [row v] to []

set [black cards total v] to [0]

set [row number v] to [1]

change [row number v] by (1)

set [black cards total v] to [0]

change [index v] by (1)

change [black cards total v] by (1)

set [row v] to (answer)

set [index v] to [1]
Hints
  • Make a variable called “black cards total” and set its value to 0. 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 6 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