I would like to print the following pattern in Python 3.5 (I\'m new to coding):
*
***
*****
*******
*********
*******
*****
***
*
simple way ...
n= 11 #input is even number 1,3,5,...
a = 1
b = 1
for a in range(n+1):
if a%2 != 0:
val = (n - a) // 2
print (" "*val + "*"*(a) + " "*val ,end = "\n")
for b in range(n-1,0,-1):
if b%2 != 0:
val2 = (n-b)//2
print (" "*val2 + "*"*(b) + " "*val2 ,end = "\n")
Output:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Or using reverse method, first one is diamond, second one is series of diamond
import copy
n = 10 #input: size of diamon
raw = []
lst = []
re_lst = []
for a in range(n+1):
if a%2 != 0:
val = (n - a) // 2
raw = ''.join(" "*val + "*"*(a) + " "*val)
lst.append(raw)
re_lst = copy.deepcopy(lst)
lst.reverse()
#print diamond
for i in re_lst:
print(i)
for i in lst:
print(i)
print("\n")
#print series of diamond
for i in re_lst:
print(i)
for i in lst:
print(i)
There are two version of this
Space between Stars
n = 4
for i in range(n):
print(' '*(n-i-1) + '* '*(i+1) )
for i in range(n):
print(' '*(i+1) + '* '*(n-i-1))
Without Space between Stars
n = 4
for i in range(n):
print(' '*(n-i-1) + '*'*((2*i)+1) )
for i in range(n):
print(' '*(i+1) + '*'*((2*((n-1)-i))-1))
def pattern2(row):
s=1
c = row / 2
d = int(c)-1
for i in range(1,row+1):
if i<c:
print(" "*d,star(s))
s+=2
d-=1
elif i==(c+0.5):
print(star(row))
s=s-2
d=0
else:
print(" "*d,star(s))
s=s-2
d+=1
def star(s):
return '*'*s
def main():
row=int(input("enter the no. of row but the rows should be odd \n#special case of pattern"))
try:
a=row%2
assert a!=0 and row!=0
pattern2(row)
except:
print('Worng selection of rows for the perfect diamond.')
if __name__=="__main__":
main()
Here is a solution base on height equals to top to the middle, or half of the height. For example, height is entered as 4(7) or 5(9) below. This method will yield odd number actual height
h = eval(input("please enter diamond's height:"))
for i in range(h):
print(" "*(h-i), "*"*(i*2+1))
for i in range(h-2, -1, -1):
print(" "*(h-i), "*"*(i*2+1))
# please enter diamond's height:4
# *
# ***
# *****
# *******
# *****
# ***
# *
#
# 3, 2, 1, 0, 1, 2, 3 space
# 1, 3, 5, 7, 5, 3, 1 star
# please enter diamond's height:5
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4 space
# 1, 3, 5, 7, 9, 7, 5, 3, 1 star
Here is another solution base on height equals to top to the bottom, or the actual total height. For example, height is entered as 7 or 9 below. When the user enters an even number for height, the diamond will be slightly slanted.
h = eval(input("please enter diamond's height:"))
for i in range(1, h, 2):
print(" "*(h//2-i//2), "*"*i)
for i in range(h, 0, -2):
print(" "*(h//2-i//2), "*"*i)
# please enter diamond's height:7
# *
# ***
# *****
# *******
# *****
# ***
# *
#
# 3, 2, 1, 0, 1, 2, 3 space
# 1, 3, 5, 7, 5, 3, 1 star
#
# please enter diamond's height:9
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4 space
# 1, 3, 5, 7, 9, 7, 5, 3, 1 star
#maybe it could help
height = eval ( input ( 'How high? ' ) )
height = int (height//2)
for i in range(1, height+1):
print(' ' *(height-i+1), '*'*i + '*' * (i-1) )
for i in range (height+1, 0, -1):
print (' ' * (height+1-i), '*' * i + '*' * (i-1))
Simplest Answer
n=5
for i in range(1,n+1):
print ((n-i)*(" ")+(i*" *"))
for i in range(n-1,0,-1):
print((n-i)*(" ")+(i*" *"))
Hope this helps some one