Hello, i am making this magic square and i am struggling with using x. when i enter "Please enter the numbers 1 through 9" and there is an x. we should add the number that hasn't been used. if the total for each row or column are the same number The square is magical. So for example I use "Please enter the numbers 1 through 9: 276/ 9 x 1 4 3 8" I get this "2 7 6
9 5 1
4 3 8
The square is magical!"
#π magic square
8 messages Β· Page 1 of 1 (latest)
@keen hatch
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Hey @keen hatch!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
def read_input():
user_input = input("-----Magic Square Checker-----\nPlease enter the numbers 1 through 9: ")
return user_input
def is_valid_input(user_input):
digits = [char for char in user_input if char.isdigit()]
if len(digits) != 9:
return False
for digit in digits:
if digits.count(digit) > 1:
return False
return True
def fill_x(user_input):
digits = [char for char in user_input if char.isdigit()]
used_digits = set(digits)
for x in range(1, 10):
if str(x) not in used_digits:
return user_input.replace('x', str(x))
def create_matrix(user_input):
matrix = [[0 for _ in range(3)] for _ in range(3)]
digits = [char for char in user_input if char.isdigit()]
k = 0
for i in range(3):
for j in range(3):
while not digits[k].isdigit():
k += 1
matrix[i][j] = int(digits[k])
k += 1
return matrix
def print_matrix(matrix):
for row in matrix:
print(' '.join(map(str, row)))
def is_magic_square(matrix):
row_sums = [sum(row) for row in matrix]
col_sums = [sum(col) for col in zip(*matrix)]
if len(set(row_sums + col_sums)) != 1:
return False
diag1_sum = sum(matrix[i][i] for i in range(3))
diag2_sum = sum(matrix[i][2 - i] for i in range(3))
if diag1_sum != diag2_sum or diag1_sum != row_sums[0]:
return False
return True
def main():
user_input = read_input()
if not is_valid_input(user_input):
print("Numbers do not make a square of size 3!")
return
if 'x' in user_input:
user_input = fill_x(user_input)
matrix = create_matrix(user_input)
print("The square is:")
print_matrix(matrix)
if is_magic_square(matrix):
print("The square is magical!")
else:
print("The square is not magical.")
if __name__ == "__main__":
main()
It will read a sequence of values from the user. The
values should consist of digits from the set {1,2,β¦,9} and at most one βxβ. The program must
check if the βxβ could be replaced by a digit so that the numbers make a 3 by 3 magic square. If
there is no βxβ, then it should check if the digits make a magic square. It will display the numbers
in a 3 by 3 matrix if possible. It will then inform the user if the matrix makes a magic square or
not. A magic square is a matrix of numbers where the sum in any direction is the same (rows,
columns, and diagonals), and the square contains the numbers from 1 to the number of squares
(in this case, the numbers 1 to 9, each one exactly once).
There are some modules to solve many equations many unknowns so u could just replace x with a different thing for each and make the equations for the rows/cols than run the solve and u got urself a magic square solver
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.