i need to print a basic diamond structure using asterix in python. the program should read the number of rows and print an output. eg-if the number of rows is 5 then there should be 3 of the big triangle and 2 of the reversed to give us 5 rows. also this code is cor python 2.7. sample output should be like if width=5
*
* *
* * *
* *
*
this is what i attempted
width=input("enter the number of rows")
for num in range(1, width-1 , 1):
print(('*' * num).center(width))
for num in reversed(range(1, width-2, 1)):
print(('*' * num).center(width))
I wasn't supposed to code that, but I like this stuff. This works for both even and odd numbers:
rows = input("enter the number of rows")
for num in range(1, rows // 2 + 1) + range((rows + 1) // 2, 0, -1):
print(' '.join('*' * num).center(rows))
Output rows = 5
:
*
* *
* * *
* *
*
Output rows = 6
:
*
* *
* * *
* * *
* *
*
EDIT
This is also nice:
for num in xrange(rows):
print(' '.join('*' * min(num + 1, rows - num)).center(rows))