Page 225 - Computer Science Class 11 Without Functions
P. 225

12 nSpaces = nRows-1
              13 nSymbols = 1
              14
              15 for row in range(1, nRows + 1):
              16     print(' ' * nSpaces, end = '')
              17     print(symbol * nSymbols)
              18     nSpaces = nSpaces - 1
              19     nSymbols = nSymbols + 2

            Sample Output:
             >>> Enter the number of rows: 6
             >>> Enter Symbol: *
                      *
                     ***
                    *****
                   *******
                  *********
                 ***********
            9.7.3 Hollow Diamond

            Now it is time to apply our learning to draw a hollow diamond pattern. Fig 9.9 shows a hollow diamond comprising
            11 rows (six rows in a hollow upper isosceles triangle and five rows in an inverted hollow isosceles triangle). To build a
            hollow upper isosceles triangle, nUpper  is set to 6. In the first row, we need 5 (=6-1) spaces followed by one asterisk.
            For the next row, we need 4 (=6-2) leading spaces. The leading spaces are followed by two asterisks separated by one
            space, in all 3 (=2×2-1)characters. For the next row, we need 3(=6-3) leading spaces. The leading spaces are followed by
                                                                                     th
            two asterisks separated by three spaces, in all and 5 (=2×3-1) characters. Thus, in i  row we need nUpper-i leading
            spaces. The leading spaces are followed by two asterisks, separated by an appropriate number of spaces. To draw the
            inverted isosceles triangle, the number of leading spaces decreases by one in each subsequent row. We incorporate
            these details into the following program.
              01 '''
              02 Objective: To display the hollow diamond
              03 Inputs:
              04     nRows : number of rows
              05     symbol: symbol to be printed
              06 Output: Hollow diamond of symbols
              07 '''
              08
              09 nRows = int(input('Enter the number of rows (odd number): '))
              10 symbol = input('Enter Symbol: ')
              11
              12 assert nRows%2==1, 'A diamond must have odd no of rows'
              13 # Upper part of hollow diamond
              14 nUpper = nRows//2 +1
              15 for i in range(1, nUpper+1):
              16     print(' ' * (nUpper-i), end = '')
              17
              18     for j in range(1, 2*i):
              19         if j==1 or j==2*i-1:
              20             print(symbol, end = '')
              21         else:
              22             print(' ', end = '')
              23     print()
              24
              25 # Lower part of hollow diamond
              26 nInverted = nRows//2
              27 for i in range(nInverted,0,-1):


                                                                                              Looping in Python  223
   220   221   222   223   224   225   226   227   228   229   230