Oct-23-2017, 04:16 PM
Hello everyone,
So I need to write a code using nested loops to draw a pyramid in the following pattern:
* 1
***** 5
********* 9
************* 13
**************** 17
So each row needs to have 4 more * then the rest. So Far I have come up with the following code:
def triangle(n):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# number of spaces k = 2 * n - 1 # outer loop to handle number of rows for i in range ( 0 , n): # inner loop to handle number spaces # values changing acc. to requirement for j in range ( 0 , k): print (end = " " ) # decrementing k after each loop k = k - 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # printing stars print ( "* " , end = "") # ending line after each row print ( "\r" ) # Driver Code n = 5 triangle(n) |
Output: *
* *
* * *
* * * *
* * * * *
When I change details within the code I cant get it to match what it should. This is just the base I am using from a different assignment. Can someone point me in the right direction?