'I'm trying to use a while loop instead of a for loop. How would I change this code so that it works using a while loop in python?
# Generating Hollow Pyramid Pattern Using Stars
row = int(input('Enter number of rows required: '))
for i in range(row):
for j in range(row-i):
print(' ', end='') # printing space required and staying in same line
for j in range(2*i+1):
if j==0 or j==2*i or i==row-1:
print('*',end='')
else:
print(' ', end='')
print() # printing new line
I've tried swapping the (for) for (while <= .... etc) and add in i = 1 and i += 1 at the end but none seem to work.
Solution 1:[1]
Based on the code above, are you sure that you are adding the integer increment within the while loop?
for example,
i = 1
while i <= 10:
do_something
i+=1
will stop after 10 cycles, but
i = 1
while i <= 10:
do_something
i+=1
will go on forever
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VonSquiggs |
