print a * pyramid - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: print a * pyramid (/thread-5826.html) |
print a * pyramid - drogers10940 - Oct-23-2017 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): # 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)This gives me an output of 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?
RE: print a * pyramid - sparkz_alot - Oct-23-2017 (Oct-23-2017, 04:16 PM)drogers10940 Wrote: When I change details within the code I cant get it to match what it should. What are you changing? RE: print a * pyramid - drogers10940 - Oct-23-2017 I have been trying to tweak the inner loop to handle the number of columns per row. When I change it to for j in range(0, i+3):I get the output of: do I need another statement to account for the addition of the extra * per line? I understand the concept of nested loops. But its these questions regarding shapes that really throw me off as I have a lot of trouble conceptualizing some type of formula/equation for them.also, there was an error in copying what it should look like in the original post, it should look as follows:
RE: print a * pyramid - drogers10940 - Oct-24-2017 After a lot of head scratching and whiskey I figured it out. I needed to add a y statement in there to account for my increase in *. Your question made me realize it sparks_alot. Thank you. I got it to work with this ( I added lots of notes to walk me through the steps) : n = 5 triangle(n) def triangle2(n): x = 8 y = 1 ## outer loop changes the row for i in range(n): for j in range(x): ## how many times to repeat the following statement is changing ## row1: 8 times, row2: 6 times, row3: 4 times, row4: 2 times, row5: 0 times ## range(x) x changes as 8 --> 6 --> 4 --> 2 --> 0 so we use statement x = x - 2 ## initially x = 8 print(" ", end="") for k in range(y): ## how many times to repeat the following statement is changing ## row1: 1 time, row2: 5 times, row3: 9 times, row4: 13 times, row5: 17 times ## range(y) y changes as 1 --> 5 --> 9 --> 13 --> 17 so we use statement y = y + 4 ## initially y = 1 print("*", end = "") x = x - 2 y = y + 4 print() triangle2(5)
RE: print a * pyramid - gruntfutuk - Oct-27-2017 You might want to type print('*' * 10) into the console.
RE: print a * pyramid - prothej227 - Apr-26-2020 Here's my way of solving the problem. Since unlike C, printing str/int is directly concatenated after the loop, while in Python, the string is printed in newline. To solve this, i just created an output string "star" and then appended all the necessary data created by the loop. row = int(input("Input the number of rows of your pyramid: ")) j = int() k = int() s = int() star = str() for j in range(1, row + 1): for s in range(row - j,0, -1): star += str(" ") for k in range(0, 2*j - 1): star +=str("*") star += str("\n") print(star) |