Print the reverse of an arrow pattern in Python

Question:

Right now this code gives me:

*
 *
  *
   *

And I can’t seem to figure out how to get the arrow finish off (in other words reversing the first print):

*
 *
  *
   *
   *
  *
 *
*

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

while columns <= 0:
    print ("Invalid entry, try again!")
    columns = int(input("How many columns? "))

x = 1
for x in range(columns):
        for x in range(x):print(" ", end="")
        print("*")
Asked By: Danny Garcia

||

Answers:

You can run loop backwards after your first loop finished. range() can take three parameters. start, stop, step. With step, you can move backwards.

for x in range(1, columns):
    for x in range(x):
        print(" ", end="")
    print("*")

for x in range(columns,0,-1): 
    for x in range(x):
        print(" ", end="")
    print("*")
Answered By: Lafexlos

For the first section (first half) just add space as it’s index and for second half add space and decrease each iterate :

 for x in range(columns):
        if(x<(columns//2)):print (" "*x+"*")
        else : print(" "*(-x+(columns-1))+"*")

 columns = 8  

*
 *
  *
   *
   *
  *
 *
*

columns = 7   

*
 *
  *
   *
  *
 *
*
Answered By: ᴀʀᴍᴀɴ

I would do it this way:

1 – I construct the list of values to adjust position of * in the print, using chain from itertools
2 – While iterating through the list, I pass the adjustment value to str.rjust

>>> from itertools import chain
>>> col = int(input('Enter nb of columns:'))
Enter nb of columns:7
>>> l = chain(range(1,col), range(col,0,-1))
>>> 
>>> for x in l:
    print('*'.rjust(x))


*
 *
  *
   *
    *
     *
      *
     *
    *
   *
  *
 *
*
Answered By: Iron Fist
v = [" ", " ", " ", " ", " ", " ", " "]

col = int(input('Enter nb of columns:'))
for x in range(1, col):
    for i in range(0,x):
        v[x] = "*"
    print x * " " ,v[x]
x = col
for x in range(x, 0, -1):
    for i in range(x,0,-1):
        v[x] = "*"
    print x * " " ,v[x]
Answered By: user3586395

How to print reverse arrow?
*
**
***
****


****
 ***
  **
   *
Answered By: AmitG
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.