+ 1
Can someone explain this I don't know how I got it
rows = int(input()) for i in range(0, rows): for j in range(0, i+1): print("*", end = ' ') print("\r") for i in range(rows, 0, -1): for j in range(0, i-1): print("*", end=' ') print("\r")
1 Odpowiedź
+ 2
Tshegofatso Sekgothe
range(start, end)
Suppose rows = 5 so range will give [0, 1, 2, 3, 4]
There is nested loop and inner loop will depends on outer loop so
If rows is 5 then outer loop will work till 5 times
And inner loop will run ith times so if i = 0 then range(0, 0 + 1) = range(0, 1)
So inner loop will work 1 time
If i = 1 then inner loop will work 2 times
i = 2 then 3 times
i = 3 then 4 times
So your pattern will be
* => i = 0, j = 1
** => i = 1, j = 2
*** => i = 2, j = 3
**** => i = 3, j = 4
***** => i = 4, j = 5
end = ' ' is used to do not print with new line
range(rows, 0, -1) will give you 4, 3, 2, 1, 0
Your loop will work in reverse order and pattern will be
****
***
**
*