Reputation: 95
My desired output is two half pyramids separated by two spaces.
length = int(input("Enter size of pyramid."))
hashes = 2
for i in range(0, length):
spaces = length - (i+1)
hashes = 2+i
print("", end=" "*spaces)
print("#", end=" "*hashes)
print(" ", end="")
print("#" * hashes)
However, this ends up printing only the first hashes of each row on the left pyramid. If I get rid of the end=
in line 7, the pyramids are both printed correctly, but with newlines after each row. Here are the outputs:
With end=:
# ##
# ###
# ####
# #####
Without end=:
##
##
###
###
####
####
#####
#####
All I want now is to have the second output, but without the newlines.
Upvotes: 0
Views: 605
Reputation: 667
Try this algorithm:
length = int(input("Enter size of pyramid."))
# Build left side, then rotate and print all in one line
for i in range(0, length):
spaces = [" "] * (length - i - 1)
hashes = ["#"] * (1 + i)
builder = spaces + hashes + [" "]
line = ''.join(builder) + ''.join(builder[::-1])
print(line)
Upvotes: 1
Reputation: 33275
You're multiplying the end
parameter by the number of hashes, instead of multiplying the main text portion.
Try this modification:
length = int(input("Enter size of pyramid."))
hashes = 2
for i in range(0, length):
spaces = length - (i+1)
hashes = 2+i
print(" " * spaces, end="")
print("#" * hashes, end="")
print(" ", end="")
print("#" * hashes)
Upvotes: 1
Reputation: 2968
The most straightforward way to print any output you want without newlines is to uses sys.stdout.write
. This writes a string to the stdout
without appending a new line.
>>> import sys
>>> sys.stdout.write("foo")
foo>>> sys.stdout.flush()
>>>
As you can see above, "foo"
is written with no newline.
Upvotes: 2