Nested Loop to Generate Triangle

MRAB python at mrabarnett.plus.com
Thu May 25 14:52:25 EDT 2017


On 2017-05-25 17:28, Victor Demelo wrote:
> 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:
> OOOO
>  OOO
>   OO
>    O
> 
> 
> I just need to get it flipped-over on the other side.
> 
> Here is the code I have written, thank you.
> 
> 
> 
> 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()
> 
On row 0 (the top row) you want 0 spaces then base_size repeats of 'O'.

On row 1 you want 1 spaces then (base_size - 1) repeats of 'O'.

On row 2 you want 2 spaces then (base_size - 2) repeats of 'O'.

Do you see the pattern?



More information about the Python-list mailing list