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)

16th Feb 2019, 4:26 PM
Ariel Shenker
Ariel Shenker - avatar
5 Réponses
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.
16th Feb 2019, 4:46 PM
Maneren
Maneren - avatar
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)
26th Jul 2020, 5:16 AM
JEEVANANDHAN K
JEEVANANDHAN K - avatar
0
@Jee kali, what is i and j representing as i know 'a' is the height
28th Jul 2020, 10:11 AM
Rydell Thomas
Rydell Thomas - avatar
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)
29th Jul 2020, 5:52 PM
Kadiveti Sai Kumar
Kadiveti Sai Kumar - avatar
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)
6th Sep 2020, 7:13 AM
Minh Duke Nguyen
Minh Duke Nguyen - avatar