Python Forum
Nested Loop to Generate Triangle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Loop to Generate Triangle
#1
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()
Reply
#2
one option is to use string formatting and specify fill option.
another one is to print the space yourself
Reply
#3
The image is broken. What's the expected output?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I think he's looking for
OOOO
 OOO
  OO
   O
maybe?
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
#6
They mentioned reversed, but I didn't know if that's horizontal or vertically reversed.
Reply
#7
Yes, that's what he wants. I have no problem to see the picture. Actually I don't even realised it is an image.
Reply
#8
If I click on the image link, I get a University's login page Big Grin
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
#9
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'.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Need help on this nested loop task PP9044 4 4,671 Apr-16-2021, 01:31 PM
Last Post: perfringo
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,886 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,393 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,966 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,111 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Triangle function program m8jorp8yne 2 8,902 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 4,948 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Nested if stmts in for loop johneven 2 5,896 Oct-19-2019, 04:05 AM
Last Post: xeedon
  Intersection of a triangle and a circle Gira 3 3,629 May-19-2019, 06:04 PM
Last Post: heiner55
  Nested for loop issue always using index 0 searching1 2 2,599 Dec-30-2018, 09:17 AM
Last Post: searching1

Forum Jump:

User Panel Messages

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