Reputation: 227
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("*")
Upvotes: 0
Views: 2134
Reputation: 21
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]
Upvotes: 0
Reputation: 10951
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))
*
*
*
*
*
*
*
*
*
*
*
*
*
Upvotes: 2
Reputation: 4528
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
*
*
*
*
*
*
*
Upvotes: 0
Reputation: 7735
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("*")
Upvotes: 0