Search code examples
pythongeometry

x not aligned with the geometric form


This is from a exercise I'm solving for my code class, I am trying to solve it without any help but tI'm not understanding how to solve this issue. GPT can't help me neither because they print stuff diferently so it's always wrong.

output:

x           x
x            x
x             x
x x x x x x x x x

output wanted:

x         x
x           x
x             x
x x x x x x x x x

code:

numeroRecebido = 5

def EsqSide(numeroRecebidoL, numeroRecebidoC, i):
    numeroRecebidoL = numeroRecebidoC - 3
    while i <= numeroRecebidoL:
        i = i + 1
        print("x", end=" ")
        spc = 0
        while(spc <= numeroRecebidoC + i):
            spc = spc + 1
            print(" ", end="")
        print("", end=" ")
        print("  x")

def baseBottom(numeroRecebido, i):
    if(numeroRecebido >= 5):
        numeroRecebido = numeroRecebido * 2
        numeroRecebido  = numeroRecebido - 2
        while(i <= numeroRecebido):
            i = i + 1
            print("x", end=" ")

    else:
        numeroRecebido = numeroRecebido * 2
        while(i <= numeroRecebido):
            i = i + 1
            print("x", end=" ")

EsqSide(numeroRecebidoL=numeroRecebido, numeroRecebidoC=numeroRecebido, i=0)
baseBottom(numeroRecebido, i=0)

Solution

  • You have a number of little inconsistencies, but they are enough to display the last x in a wrong place:

    • you start with spc = 0 while the initial x has used one position
    • you increment i before displaying the line when you should do that after
    • you only add one single space with print(" ", end="") while the bottom line has 2 positions per element (one space and one x)
    • you have added a extraneous print("", end=" ") probably as an inefficent attempt to fix the wrong position of the last x

    Here is a fixed version:

    def EsqSide(numeroRecebidoL, numeroRecebidoC, i):
        numeroRecebidoL = numeroRecebidoC - 3
        while i <= numeroRecebidoL:
            print("x", end=" ")
            spc = 1
            while(spc < numeroRecebidoC + i):
                spc = spc + 1
                print(" ", end=" ")
            print("x")
            i = i + 1
    

    With it, I could get:

    x         x
    x           x
    x             x
    x x x x x x x x x