My True or False function is working but how do I alternate the rows with consistent spacing in the True statement?

42 views Asked by At

I have tried rearranging the code around and I am very close to figuring this out but in my True statement the results are close to aliging correctly but I can't figure it out. This homework assignment requires that the rows alternate with a space if True but if False they must form a square.

My code:

def get_brick_pattern(max_rows=1, max_cols=1, running_bond=True or False):
    brick = ""
    result = ""
    columns = max_cols
    rows = max_rows
    if running_bond is True:
        for rows in range(max_rows):
            for columns in range(max_cols):
                if (rows) % 2 == 0:
                    result += brick
                else:
                    result += " " + brick
            result += "\n"
        return result
    elif running_bond is False:
        for rows in range(1, max_rows+1):
            for columns in range(1, max_cols+1):
                result += brick
            result += "\n"
        return result

print(get_brick_pattern(3, 6, True))---for testing

The results are supposed look like just as an example:


  

My current output for the True statment looks like this:


      

2

There are 2 answers

0
Barmar On

You shouldn't add a space before each brick in the inner loop, just at the beginning of the line. Move the if rows % 2 == 0: check to the outer loop.

Then you don't need the inner loop at all, since you can use * to repeat a string N times.

        for rows in range(max_rows):
            if row % 2 == 1:
                result += " "
            result += brick * max_cols
            result += "\n"
        return result
0
Reilas On

"... the rows alternate with a space if True but if False they must form a square ..."

Check rows % 2 before the inner-loop.

for rows in range(max_rows):
    if (rows) % 2: result += ' '
    for columns in range(max_cols):
        result += brick
    result += "\n"

Output, for 3, 6.