Programs

Implement a "Magic Square", where someone selects a particular letter on the screen, and the computer tells them what they chose. The video shown below is very similar to the version we have in class, however the table values themselves are different. For Program 3: Magic Square run your test cases and submit your code in Zybooks 5.35 ( You can also run a version of this program built in Code.org's App Lab at bit.ly/uicmindreader )

Running the program should look like the following:

For the non-extra credit version you should always enter 0 as the level, as shown above.

The secret to the program (shh... don't tell) is that the program itself chooses a random "magic character" ahead of time, which in the case of the table above is the character 'E'. For any number, if you subtract its individual digits from itself, then you end up with a number that is evenly divisible by 9. When the table is printed, in all the numbered locations below 90 where 9 is a factor (9, 18, 27, 36, 45, 54, 63,72, 81) the special character 'E' that was chosen ahead of time is displayed there. For all the rest of the characters a random character is chosen.

What you Need to Know

You need to know the following Python concepts:

  1. Formatting output using print, including printing on the same line (using end = ' ' as part of the print statement) and explicitly printing a new line (using print() )

  2. How to use a while loop to repeat instructions

  3. How to use an if statement to conditionally execute code

  4. How to generate a random letter, using random.randint(a, b). Random number generators need a random number seed. In your program after the user has entered the desired level, use the level value as the seed for the random number generator. For the standard version of the program you should enter 0 as the level. This value of 0 should be stored into variable level and then be used to initialize the random number generator as follows: random.seed( level). This will make your output predictable, so that it will match the expected test cases output.

  5. How to convert a number into a character using the chr() function discussed in Zybook 2.10.

  6. How to see if a number is evenly divisible by some other number using mod (%). For instance to see if a number if evenly divisible by 10 (such as is the case at the end of every line) you would use: if counter % 10 == 0:If you are trying to check if a counter value is evenly divisible by 9 you would use: if counter % 9 == 0:

  7. Getting input from the user

  8. How to get a random upper or lower case character using the function get_Random_Letter() shown below. When you call this function you need to store the result into some variable (e.g. the_secret_letter = get_Random_Letter() ), or use it in a context where you need a character, such as in a print statement (e.g. print( get_Random_Letter()) ).

The function used to get a random letter is shown below. This is used once at the beginning of the program to select the secret_character, and is also called once for every table location that is below loop counter value of 90 that is not divisible by 9, to get a random character to be displayed.

Here is some sample code illustrating some of the concepts needed to write this program. You do not need to start with this code. It is just provided to demonstrate some of the concepts needed for this program.

Write your program following these Steps

  1. Display the header lines at the top of the program

  2. Use a while loop to count from 99 down to 0. Display just those numbers, with a colon ':' and then a space after each one.

  3. Add code using the mod (%) function so that you print a new line character after each set of 10 output values. This will result in a row of numbers from 99 down to 90, a second row from 89 down to 80, and so on.

  4. After each number now also get a random letter by calling get_Random_Letter() (see below) and print that letter after the colon.

  5. Declare and use a variable for the secret letter. To do this, at the beginning of your program call function get_Random_Letter()and store the result into the secret letter variable (e.g. secret_char = get_Random_Letter() ). So that your output matches the expected output in the test cases, you should call the random number generator the minimum number of times. When your program starts, as explained above you should call function get_Random_Letter() once to find and store the secret letter, as explained above. Then for each table entry that is not less than 90 and evenly divisible by 9) you will again call get_Random_Letter(). You should not call the random number generator besides this, so that your output matches the expected output.

  6. Add code at the spot in your program where you display a random character for each table entry. If the loop counter is less than 90 and the counter value is evenly divisible by 9, then display the already-chosen secret_char and not a random character.

  7. Add the text and prompt at the bottom of the program.

Points

The program is worth 10 points as follows:

  • 7 points coming from test cases

  • 3 points from having good comments in your program. For full credit you should have:

    • Header comments describing what the program does

    • Comments in each main section of your program, making it clear what section does.

Extra Credit Version

The extra credit version output and video are shown below. Extra credit can earn you 3 extra points.

Extra Credit Version Notes

  1. As previously explained, make sure that you use the level number as the seed for the random number generator. Each time the same level number is used, the resulting grid and selected secret letter should be the same as previous program runs when that same level number was used.

  2. The number of extra lines displayed after the output Building neural network is equal to the value of level, with a maximum of 5. In the above example the level was 4, so there were 4 lines, each with a single period on it. After each of these lines is displayed your program will wait for one second, using the time.sleep( 1) command. For this to work you will also need to use import time at the top of your program.

  3. In the extra credit version the lower the level, the lower the probability of the program getting the right answer. The higher the level, the higher the probability of getting the right answer, with an upper bound of 5.

    1. The probability of getting the correct answer for each level are as follows: 1: 20%, 2: 40%, 3: 60%, 4: 80%, 5+: 100%.

    2. To implement probabilities of a right answer:

      1. Generate a random number between 0..1 using random().

      2. Then check this random number value from the previous step to determine whether or not to display the secret letter that the computer already knows. So if the level is 1, then if the random number is <= 20% print the secret letter, else print a new random character obtained using a new call to get_Random_Letter(). Likewise if the level is 2, then if the random number is <= 40% print the secret letter, else print a new random character, and so on.