0

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("*")
1
  • Why you tagged design-patterns ? THIS IS NOT DESIGN PATTERN! Commented Feb 29, 2016 at 13:26

4 Answers 4

2

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))


*
 *
  *
   *
    *
     *
      *
     *
    *
   *
  *
 *
*
Sign up to request clarification or add additional context in comments.

Comments

0

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("*")

Comments

0

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   

*
 *
  *
   *
  *
 *
*

Comments

0
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.