# Python pattern challenge - Day 4

n = 7

d = n // 2 + 1

for x in range(1, n + 1):

for y in range(1, n + 1):

if x == n // 2 + 1 or y == d:

print("*", end="")

else:

print(" ", end="")

if x <= n // 2:

d += 1

else:

d -= 1

print()

Let's break down the code step by step:

n = 7: This line initializes a variable n with the value 7. This value represents the size of the pattern, specifically the number of rows and columns.

d = n // 2 + 1: This line calculates the starting position for printing the asterisks in each row. Since the asterisks form a cross pattern, the distance from the left edge to the vertical center of the cross (d) is set to half of the size of the pattern plus 1. This calculation ensures that the cross is properly centered horizontally.

The nested loops:

for x in range(1, n + 1):: This outer loop iterates over each row of the pattern.

for y in range(1, n + 1):: This inner loop iterates over each column of the pattern within the current row.

Inside the nested loops, there's an if-else statement:

if x == n // 2 + 1 or y == d:: This condition checks if the current position (x, y) is on the horizontal center line (x == n // 2 + 1) or the vertical center line (y == d). If the condition is true, an asterisk is printed.

else:: If the condition is false (i.e., the current position is not on the center lines), a space is printed.

After printing each row, there's an adjustment to the variable d:

if x <= n // 2:: This condition checks if we are still in the upper half of the cross. If true, it means we need to move the vertical center (d) downwards for the next row.

else:: If we are in the lower half of the cross, we need to move the vertical center (d) upwards for the next row.

The print() statement at the end of the outer loop prints a newline character, moving to the next row in the pattern.

Overall, this code generates a cross pattern made of asterisks (*) with the specified size (n) and ensures that the cross is properly centered both horizontally and vertically.