I am new to Python and wanted to create a program which prints this star pattern using one for loop and an if-else statement:
*
**
***
****
*****
****
***
**
*
What I’ve done so far:
stars = "*"
for i in range (0,10):
if i <5:
print(stars)
stars = stars + "*"
# else:
Output:
*
**
***
****
*****
Not sure what to put in the else statement?
I’ve tried looking online but all solutions I’ve seen use two for loops, which makes sense. Is it possible to use one loop?
>Solution :
What you should do is is to increase the number of stars in each row up to a certain point, and then decrease the number of stars in each subsequent row.
Here’s how you can do it:
stars = "*"
for i in range(1, 9):
if i <= 4:
print(stars)
stars = stars + "*"
else:
print(stars)
stars = stars[:-1]
print("*")