Python Forum

Full Version: Hollow triangle-drawing characters param error.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I thought my algorithm was perfect, but I was wrong.

Here's my code :

r = int(input())
i = 0

print("@", end="\n")
while i < r-2:

    print("@" + (" "*(i) + "@"),end = "\n")
    if i == r-3:
        print("@"*(i+3), end="\n")

    i +=1
I need to have this drawing if I put the number "6" in "r" parameter and I have It:

@
@@
@ @
@  @
@   @
@@@@@@
But, when I put the number "2" I get this :

@
Instead of :

@
@@
Thanks for any help. Blush
You initialize i to 0. If you input 2, line 5 compares 0 to 2 - 2, and 0 is not less than 0. So it never goes through the loop.
for rows in range(1, 10):
    if rows > 2:
        print('@')
        for i in range(rows-2):
            print(f'@{i*" "}@')
        print('@'*rows)
    else:
        for i in range(1, rows+1):
            print('@'*i)
    print()
r = int(input())
i = -1
 
print("@", end="\n")
while i < r-2:
    if i == -1:
        i = 0
    print("@" + (" "*(i) + "@"),end = "\n")
    if i == r-3:
        print("@"*(i+3), end="\n")
 
    i +=1
I tried replacing "<" with "<=" which worked with 2, but not with other numbers such as 5. So it has to go through a less than loop at least once, but still be 0. This is the solution I came up with. If it is negative 1, it will go through the loop. Then if i == -1 (first time going through the loop), it sets it back to 0.
I now understand why "i" must be negative, as ichabod801 said.
Now it works.
Thank you for your help.