In: Computer Science
pyramid.py program
for i in range(1,6):
for j in range(i):
print("*",end=' ')
print("\n",end='')
Exercise: Create the pyramid similar to the above pyramid.py program using a while loop. Name your program whilepyramid.py and submit it on blackboard.
Here is the code for the given question using while loop:
whilepyramid.py
*********************************************************************************************************************************************
i = 1 #initialising a variable i to 1 as we have to print in
range 1 to 6 so start with 1
while i < 6: #continue the loop untill i is less than 6 as
ending range is 6
j = 1 #initialising a variable j to 1
while j <= i: #checking if j is less than or equal to i
print("*",end=' ') #print statement for a row
j+=1 #increment j
print("\n",end = '') #print statement for outer loop i.e change
row
i+=1 #increment i
**********************************************************************************************************************************************
Providing screenshot of the code for further clarification
Output of my code :
**********************************************************************************************************************************************
For your verification i have provided your code that is by the for loop with its output:
for loop code:
pyramid.py
Output of this above code