'triangle drawing code: It is drawing a duplicate at the end and I am not sure why
I was writing a program that takes user input for a character and a triangle height and uses those to draw a triangle using Python code. It is drawing what I want, but it is also drawing another line of characters right below the last line for some reason. I was practicing nest-loops.
This is the code that I am using:
triangle_char = input('Enter a character:\\n')
triangle_height = int(input('Enter triangle height:\\n'))
count1 = 0
while count1 \< triangle_height:
print(triangle_char)
count2 = 0
while count2 \<= count1:
print(triangle_char, end=' ')
count2 = count2 + 1
count1 = count1 + 1
Here is what I am getting...
Enter a character:
Enter triangle height:
%
% %
% % %
% % % %
% % % % %
% % % % %
Solution 1:[1]
A line with characters will only be printed (as a line) when a newline is printed. Which happens in the next iteration of the outer loop, since the inner loop has end=' '. But for the last iteration, that print(triangle_char) is not executed, and as a result, the last line misses one extra character (and a newline).
A solution is to put a final print(triangle_char) outside both loops. That will result in one extra line, so the outer condition will need to change:
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
count1 = 0
while count1 < triangle_height-1:
print(triangle_char)
count2 = 0
while count2 <= count1:
print(triangle_char, end=' ')
count2 = count2 + 1
count1 = count1 + 1
print(triangle_char)
But I would just suggest to use for-loops instead:
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
for i in range(triangle_height-1):
print(triangle_char)
for _ in range(i+1):
print(triangle_char, end=' ')
print(triangle_char)
Or use the str.join() functionality:
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
for i in range(1, triangle_height+1):
print(" ".join(triangle_char for _ in range(i)))
(requires a slight change in limits again)
Of course, this being Python, you can one-line the above to
triangle_char = input('Enter a character: ')
triangle_height = int(input('Enter triangle height: '))
print("\n".join(" ".join(triangle_char for _ in range(i)) for i in range(1, triangle_height+1)))
Solution 2:[2]
You're very close. The main issue here is that you should print a newline by itself to finish each row, and the print(triangle_char) statement in the main loop should be removed. You would get:
count1 = 0
while count1 < triangle_height:
count2 = 0
while count2 <= count1:
print(triangle_char, end=' ')
count2 = count2 + 1
print()
count1 = count1 + 1
To simplify this further, you could replace your while loops by instead using for loops over a range:
for count1 in range(triangle_height):
for count2 in range(count1 + 1):
print(triangle_char, end=' ')
print()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 9769953 |
| Solution 2 | flakes |
