Reputation: 21
I keep getting blank lines when executing my code, I'm using a for loop to create this pattern
, but i need to get rid of the spaces between the printed lines.
This is the code im using
n = int(input())
a = "_"
b = "|"
for i in range(0,n,1):
if i == 0:
print(" " * (2*n - 1) + "_*_")
print(" " * 2*((n - 1)) + b + " " * 3 + b)
else:
print(" " * ((2*n - 1) - 2*i) + a + (" " * ((i + 1) * 4 - 3) ) + a)
print(" " * ((2*(n-1)) - 2*i) + b + (" " * ((i + 1)*4 - 1)) + b)
Upvotes: 2
Views: 96
Reputation: 703
To add to flakes' answer, this is because the print() prints on a new line. This is why there is a space : instead of being on the same line, both caracters are on a separate line. As such, you should have a and b in the same print statement as shown by flakes
Upvotes: 0
Reputation: 23624
Lets break this down a bit and come up with some equations. First lets examine a possible output to see what the padding needs to be on the outer and inner parts of the pyramid.
_*_ n=5, i=0, outer=7, inner=0
_| |_ n=5, i=1, outer=5, inner=3
_| |_ n=5, i=2, outer=3, inner=7
_| |_ n=5, i=3, outer=1, inner=11
| | n=5, i=4, outer=0, inner=15
For the inner, if i == 0
then inner = 0
otherwise inner = (4 * i) - 1
For the outer, if n - i == 1
then outer = 0
otherwise outer = (2 * (n - i - 1)) - 1
Now we can code this together:
for i in range(n):
outer = " " * ((2 * (n - i - 1)) - 1)
inner = " " * ((4 * i) - 1)
if i == 0:
# inner is 0
print(outer + "_*_")
elif n - i == 1:
# outer is 0
print("|" + inner + "|")
else:
print(outer + "_|" + inner + "|_")
Upvotes: 1