Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Required Functions You are required to write and use the following three functions in your submission: def populateSquare ( size ) : this function takes

Required Functions
You are required to write and use the following three functions in your submission:
def populateSquare(size): this function takes an integer representing the
size of the square, asks the user to enter the integer values to populate the square
and it returns the square. To simulate the square, we will use a list of lists: this is a
list in which each element is another list. Each value entered by the user can only
appear one single time in the square (so, no repeated values)
def printSquare(square): this function takes the filled square (so the list
of lists) and displays it into the screen, one row per line. This function does not
return anything.
def isMagicSquare(square): this function takes the filled square (so the list
of lists) and returns a Boolean that indicates whether or not the square is magic
square as per the rules explained above.
Note: while solving this problem, you may find the need to create one or more helper
functions (for example to validate user input). That is fine and you can add those
functions to your program.
Program Examples
Example 1(users input is shown in red):
Welcome to the Magic Square Game
Enter the size of your square (between 3 and 9): 3
Row 1
Enter a value for the square: 4
Enter a value for the square: 9
Enter a value for the square: 2
Row 2
Enter a value for the square: 3
Enter a value for the square: 5
Enter a value for the square: 7
Row 3
Enter a value for the square: 8
Enter a value for the square: 1
Enter a value for the square: 6
Here is your square:
492
357
816
This is a magic square!!
Example 2(users input is shown in red):
Welcome to the Magic Square Game
Enter the size of your square (between 3 and 9): 3
Row 1
Enter a value for the square: 1
Enter a value for the square: 2
Enter a value for the square: 3
Row 2
Enter a value for the square: 4
Enter a value for the square: 5
Enter a value for the square: 6
Row 3
Enter a value for the square: 7
Enter a value for the square: 8
Enter a value for the square: 9
Here is your square:
123
456
789
This is not a magic square.
Example 3(users input is shown in red):
Welcome to the Magic Square Game
Enter the size of your square (between 3 and 9): 4
Row 1
Enter a value for the square: 16
Enter a value for the square: 3
Enter a value for the square: 2
Enter a value for the square: 13
Row 2
Enter a value for the square: 5
Enter a value for the square: 10
Enter a value for the square: 11
Enter a value for the square: 8
Row 3
Enter a value for the square: 9
Enter a value for the square: 6
Enter a value for the square: 7
Enter a value for the square: 12
Row 4
Enter a value for the square: 4
Enter a value for the square: 15
Enter a value for the square: 14
Enter a value for the square: 1
Here is your square:
163213
510118
96712
415141
This is a magic square!!
Example 4(users input is shown in red):
Welcome to the Magic Square Game
Enter the size of your square (between 3 and 9): 2
Value out of bounds. Please try again.
Enter the size of your square (between 3 and 9): 10
Value out of bounds. Please try again.
Enter the size of your square (between 3 and 9): abc
Invalid input. Please try again.
Enter the size of your square (between 3 and 9): 3
Row 1
Enter a value for the square: 1
Enter a value for the square: 1
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 1
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 0
Value out of bounds. Please try again.
Enter a value for the square: 10
Value out of bounds. Please try again.
Enter a value for the square: xyz
Invalid input. Please try again.
Enter a value for the square: 5
Enter a value for the square: 9
Row 2
Enter a value for the square: 8
Enter a value for the square: 3
Enter a value for the square: 4
Row 3
Enter a value for the square: 6
Enter a value for the square: 7
Enter a value for the square: 2
Here is your square:
159
834
672
This is not a magic square.
Example 5(users input is shown in red):
Welcome to the Magic Square Game
Enter the size of your square (between 3 and 9): 3
Row 1
Enter a value for the square: 1
Enter a value for the square: 2
Enter a value for the square: 3
Row 2
Enter a value for the square: 1
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 2
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 4
Enter a value for the square: 4
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 5
Enter a value for the square: 6
Row 3
Enter a value for the square: 4
Error: this value already exists in the square. Enter a different
value.
Enter a value for the square: 7
Enter a value for the square: 8
Enter a value for the square: 9
Here is your square:
123
456
789
This is not a magic square.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions