0

So, for my programming class I am supposed to write a program that outputs this:

oooooo
ooooo
oooo
ooo
oo
o

I have been able to make it so that the program outputs the triangle right side up but even after looking for help online, I haven't been able to find the solution to turning the triangle upside down. Here is my code:

def main():

base_size = 6

for r in range (base_size):
    for c in range (r + 1):
        print('o', end = '')
    print()

main()

And this is the output I get:

o
oo
ooo
oooo
ooooo
oooooo

Can someone help me out? I am a newbie so this is kind of tough for me even though it's probably pretty simple for you guys.

Thanks in advance!

dansalmo
  • 11,506
  • 5
  • 58
  • 53
AvenNova
  • 337
  • 2
  • 6
  • 16

2 Answers2

5

You're very close. You can use the range function, but step using -1 to walk from n to 0.

You can also make a string by multiplying a character by an integer.

def triangle(n):
    for i in range(n, 0, -1):
        print('o' * i)

Testing

>>> triangle(6)
oooooo
ooooo
oooo
ooo
oo
o
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Range with a negative step will do the trick (end, ini, step) with step=-1, also you can eliminate one for loop by using lists and join instead:

def trig(base):
   for i in range(base, 0, -1):
       l = ['o']*i
       print(''.join(l))

Edit: @Cyber answer is much better, didn't know you could use "times" operator directly at the string.