Python Forum
Hollow triangle-drawing characters param error.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hollow triangle-drawing characters param error.
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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()
Reply
#4
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.
Reply
#5
I now understand why "i" must be negative, as ichabod801 said.
Now it works.
Thank you for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad triangle numbers Woody_MC_2022 5 1,197 Sep-24-2022, 08:14 PM
Last Post: deanhystad
  Numpy error while filling up matrix with Characters august 4 1,847 Apr-13-2022, 10:28 PM
Last Post: august
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,677 May-15-2020, 01:37 PM
Last Post: snippsat
  Fill a value in triangle shape in Matrix lynx 0 1,871 Dec-07-2019, 06:32 AM
Last Post: lynx
  Printing out a triangle using nested for loops MrGoat 12 6,561 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Triangle: max path sum Mateoo 1 2,928 Jan-10-2019, 09:16 PM
Last Post: stullis
  tf.gfile.FastGFile error unicode ( japanese characters ) majinbuu 2 3,077 May-13-2018, 02:11 PM
Last Post: majinbuu
  Area of a triangle OmarSinno 8 5,741 Sep-25-2017, 08:10 PM
Last Post: OmarSinno
  'string index out of range' error when code replaces characters Ivan 1 3,891 Mar-04-2017, 05:04 PM
Last Post: Ivan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020