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
whenclicked
askHow many rows would you like to enter?andwaitaskjoinjoinjoinPlease enter numberofrows cards for row rownumberandwait
setblack cards totalto0setrow numberto1setindexto1setnumber of rowstoanswersetrowtoanswerchangeblack cards totalby1changeindexby1changerow numberby1setblack cards totalto0
sayjoinThere is a parity error is row rownumberfor2secssayjoinjoinRow rownumber is OK!for2secs
ifletterindexofrow=Bthenrepeatnumberofrowsrepeatlengthofrowifblackcardstotalmod2=0thenelse
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 letter1ofworld block under “Operators”. For example: letter1ofworldw

  • 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 lengthofworld 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: 37mod107

Show Scratch solution