Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyramids reduction
#1
The problem is that I'm supposed to make a pyramid which reduces in size till reaching zero.

base_size=input("base_size:" )
base_size=int(base_size)
for r in range(base_size):
    for c in range(r+1):
        print('*',end='')
    print()
So far I can easily create one but I'm not sure how to make each version smaller than the last.
Reply
#2
You can make a range count backwards
https://docs.python.org/3/library/stdtypes.html#range Wrote:class range(start, stop[, step])
for value in range(5, 0, -1):
       print(value)
Output:
5 4 3 2 1
Reply
#3
This kind of worked. However,

base_size=input("base_size:" )
base_size=int(base_size)
for r in range(base_size,0,-1):
    for c in range(r+1):
        print('*',end='')
    print()
print()
Output:
base_size:6 ******* ****** ***** **** *** **
It turns it upside down. I need it to stay with the smallest number at the top and keep repeating this pattern till it gets to zero.

*
**
***
****
*****

*
**
***
****

*
**
***

*
**

Something like this.
Reply
#4
Ok either i misunderstood or you didn't describe very clearly what was required Think

As you can create a single pyramid use a reversed range to alter base_size
Reply


Forum Jump:

User Panel Messages

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