0
how python calculation this code i not understand why when i put (block 2 the height is 1) or (block 3 the height is 2 i) help p
blocks = int(input("Enter the number of blocks: ")) height = 0 inlayer = 1 while inlayer <= blocks: height += 1 blocks -= inlayer inlayer += 1 print("Height of the pyramid:",height)
5 odpowiedzi
0
It takes count of blocks, and each iteration it subtracts number of blocks in layer from it until it is less than number of required blocks to make a layer which is increasing each iteration and during this it increases hight as well.
0
Understand and use the below code:
blocks = int(input("Enter the number of blocks: "))
i=1
j=0
while True:
j=j+i
if j<blocks:
b=i
i+=1
elif j==blocks:
a=i
break
elif j>blocks:
a=b
break
print("The height of the pyramid:",a)
0
@Jee kali, what is i and j representing as i know 'a' is the height
0
Hi Rydell,
U can try this.
blocks = int(input("Enter the number of blocks: "))
#
# Write your code here.
#
counter = 0
height = 0
total_blocks = 0
while total_blocks <= blocks:
total_blocks = total_blocks + counter + 1
counter += 1
height = counter - 1
print("The height of the pyramid:", height)
0
blocks = int(input("Enter the number of blocks: "))
i=0
j=1
while i<blocks:
j+=1
i+=j
else:
print("The height of the pyramid:", j-1)