Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print a * pyramid
#1


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
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?
Reply
#2
(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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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:
Output:
* * * * * * * * * * * * * * * * * * * * * * * * *
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:
Output:
* ***** ********* ************* *****************
Reply
#4
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)
Output:
* ***** ********* ************* *****************
Reply
#5
You might want to type print('*' * 10) into the console.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyramid of leters of n levels DanyMont1505 4 2,166 Sep-27-2019, 07:56 PM
Last Post: woooee
  Pyramid gamingingrs 1 2,276 Oct-16-2018, 02:56 PM
Last Post: j.crater
  Pyramid of Solitaires qwerty 1 3,448 Mar-22-2017, 04:10 PM
Last Post: nilamo
  Implementation Pyramid Solitaire qwerty 2 5,048 Feb-27-2017, 11:57 AM
Last Post: qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020