Im stuck with my code. I have to build a pyramid with hash blocks the first row has to start with 2 hash blocks. Every row beneath 1. How do I implement this program
height = input("What height should the pyramid be?")
height = int(height)
while height > 23 :
height = input("What height should the pyramid be?")
height = int(height)
for i in range(0, height):
print("#" * height)
print()
CodePudding user response:
height = input("What height should the pyramid be: ")
height = int(height)
for i in range(0, height):
for j in range(0,i):
print("#",end =" ")
print()
This is the right loop.
output:
What height should the pyramid be: 9
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
EDIT FOR PYRAMID NOT A TRIANGLE
rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows 1):
for space in range(1, (rows-i) 1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k = 1
k = 0
print()
CodePudding user response:
thanks for the help im a bit further.
height = input("What height should the pyramid be: ") height = int(height)
for i in range(0, height): for j in range(0,i): print("#",end =" ") print()
This is a good start of the code but for example. If the height of the pyramid is 5, the program should print 6 rows. The first one starts with 2 hash blocks, the second one 3 blocks etc, adding one hash block
CodePudding user response:
After a while of thinking, I found your code. This will not create a triangle or any of the sort but it will create a proper pyramid:
height = int(height)
while height > 23:
height = input("What height should the pyramid be?")
height = int(height)
c = height
for j in range(height):
for i in range(j):
print("#", end=" ")
height -= 1
print()
for h in range(c):
print(" ", end="")
c -= 1
Here what I have done is, first get the number of spaces needed to enter in the start of the line. Then it will enter the number of hashes required for the pyramid and it will repeat this until the final pyramid is made. But just be careful, you have to enter one number plus the number of the pyramid rows you want. Otherwise it will create a pyramid of the number -1. So you can change the code in the for loop by doing height 1
or you could just leave it like that.