Write a Python program to print an empty square based on size Size = 8
- User
Write a Python program to print an empty square based on size Size = 8
Assistant
Write a Python program to print an empty square based on size Size = 8
Analyze the Pattern:
********).* *).Determine the Logic:
size - 1). Let's call the iterator i.size - 1). Let's call the iterator j.*):
i == 0)i == size - 1)j == 0)j == size - 1)*. Otherwise, print a space .Drafting the Code:
size = 8
for i in range(size):
for j in range(size):
# Check borders
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print() # Newline after each row
end=" " is used in the print statements to ensure the characters are separated by a space for better visibility, making the square look more proportional.Here is the Python program to print an empty square.
size - 1).size - 1).* only if we are on the first row, last row, first column, or last column. Otherwise, we print a space.# Set the size of the square
size = 8
# Outer loop for rows
for i in range(size):
# Inner loop for columns
for j in range(size):
# Print '*' if it is a border position, otherwise print a space
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end=" ")
else:
print(" ", end=" ")
# Move to the next line after printing a row
print()
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *