Python Forum
Nested Loop to Generate Triangle - 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: Nested Loop to Generate Triangle (/thread-3464.html)

Pages: 1 2


Nested Loop to Generate Triangle - Babbare - May-25-2017

I need the triangle to be in reverse. The assignment requires a nested loop to generate a triangle with the user input of how many lines.

Currently, I get answers such as:
OOOO
OOO
OO
O

When I actually need it to be like this:
[Image: py44.PNG?_&d2lSessionVal=8jTpwyZ1HW0PJjQYXYGzboh6H]

I just need to get it flipped-over on the other side.

base_size = int(input("How many lines? "))
columns = base_size

for row in range (base_size) :
   for columns in range (columns) :
       print('O', end='')
   print()



RE: Nested Loop to Generate Triangle - buran - May-25-2017

one option is to use string formatting and specify fill option.
another one is to print the space yourself


RE: Nested Loop to Generate Triangle - nilamo - May-26-2017

The image is broken. What's the expected output?


RE: Nested Loop to Generate Triangle - ichabod801 - May-26-2017

I think you want the first loop to be over range(base_size), and the second loop to be over range(rows). But as nilamo says, a better idea of the expected output would help.


RE: Nested Loop to Generate Triangle - sparkz_alot - May-27-2017

I think he's looking for
OOOO
 OOO
  OO
   O
maybe?


RE: Nested Loop to Generate Triangle - nilamo - May-28-2017

They mentioned reversed, but I didn't know if that's horizontal or vertically reversed.


RE: Nested Loop to Generate Triangle - buran - May-28-2017

Yes, that's what he wants. I have no problem to see the picture. Actually I don't even realised it is an image.


RE: Nested Loop to Generate Triangle - sparkz_alot - May-28-2017

If I click on the image link, I get a University's login page Big Grin


RE: Nested Loop to Generate Triangle - Babbare - May-28-2017

Sorry guys, you're all correct. The imagine is reverse from what I need, so the code has to be just starting on the opposite side.

(May-26-2017, 09:21 PM)ichabod801 Wrote: I think you want the first loop to be over range(base_size), and the second loop to be over range(rows). But as nilamo says, a better idea of the expected output would help.
Yes, you're correct. This is exactly what I am looking for,

(May-27-2017, 10:28 PM)sparkz_alot Wrote: I think he's looking for
 OOOO  OOO   OO    O 
maybe?
Yes, sorry. It's like this:

It starts from like the right side and then stacks each new line from right to left instead of left to right.
Example:

four lines to start
then three lines from right to left
two lines from right to left
and then the last line.

It will use all '0,' so each line from right to left will be 'O'.


RE: Nested Loop to Generate Triangle - buran - May-28-2017

so what you have tried, based on my suggestion in post #2?
If you expect someone to do this for you - you are mistaken.

some examples

n=7
x=4
# example 1
print('{: >{width}s}'.format('O'*x, width=n))

# example 2
print('{}{}'.format(' '*(n-x),'O'*x))

# example 3
print(' '*(n-x), end='')
print('O'*x, end='')
print()
the last one, which is the worst one can be done with even more loops

output

Output:
OOOO   OOOO   OOOO
Now it's up to you to wrap one of these in a loop