python Exercise 7: diamond print

# Print diamond (diagonal is odd, 7), as follows:

   *
  ***
 *****
*******
 *****
  ***
   *


Solution: symmetrical rhombic structure, the diamond is divided into two parts, this time the key is to identify the boundaries

Each loop can be used for printing two upper and lower halves (not recommended)

May be used for a loop, nested if statements determines

Methods: Print diagonal length of the diamond 7

for i in range (-3,4): # the diagonal line as the boundary 0
    IF I <= 0: upper half of the print #
        print ( '' * (- i ) + '*' * (7 + 2 * i ))
    the else: the lower half of the print #
        print ( '' * i + ' *' * (7-2 * i))


According to the above embodiment can be modified for a given diagonal of any rhombus odd

n = int (input ( 'Enter diamond diagonal length:'))
for I in Range (-n 2 //, // n-2 +. 1):
    IF I <= 0:
        Print ( '' * (- I) + '*' * (n-2 * + I))
    the else:
        Print ( '' + I * '*' * (n-I-2 *))



Guess you like

Origin blog.51cto.com/wszzdanm/2411557