Page 159 - Information_Practice_Fliipbook_Class11
P. 159

Sample Output:

             >>> Enter the number of rows: 6
             >>> Enter Symbol: *
                 *
                 **
                 ***
                 ****
                 *****
                 ******
            Topic 6.7.1 shows how to use nested for statements, there was a simpler way to do it using the repetition operator
            *. Recall that the expression symbol*i yields a string of length i. So, using the repetition operator, Program 6.11
            may be rewritten as Program 6.12.
            Program 6.12 To display the right triangular pattern of a symbol using string.

              01 '''
              02 Objective: To display the inverted isosceles triangular pattern of a symbol
              03 Inputs:
              04     nRows : number of rows
              05     symbol: symbol to be printed
              06 Output: Pattern
              07 '''
              08 nRows = int(input('Enter the number of rows: '))
              09 symbol = input('Enter Symbol: ')
              10 for i in range(1, nRows + 1):
              11     print(symbol*i,end='')
              12     print()
            6.7.2 Inverted Isosceles Triangle
            Next, let us write a program to print an inverted isosceles triangle using a given symbol (say, symbol). Program 6.13
            shows an inverted isosceles triangle comprising six rows using asterisks. Note that the first row does not have any
            leading spaces, the 2  row has one leading space, 3  row has two leading spaces, and so on. Finally, the last row (6th
                                                          rd
                              nd
            row) has 5 (=6-1) spaces. Thus, beginning with zero leading spaces in the first row, the number of leading spaces (say,
            nSpaces) increases by one in each following row. Furthermore, the first row has 11 (=6×2-1) asterisks, the second
            row has 9 asterisks, and so on. Finally, the sixth row has one asterisk. Thus, beginning with 2 × nRows-1 asterisks in
            the first row, the number of asterisks (say, nSymbols) decreases by two in each following row. We incorporate these
            details into the following program.
            Program 6.13 To display the inverted isosceles triangular pattern of a symbol

              01 '''
              02 Objective: To display the inverted isosceles triangular pattern of a symbol
              03 Inputs:
              04     nRows : number of rows
              05     symbol: symbol to be printed
              06 Output: Pattern
              07 '''
              08 #Approach: In each line,
              09 #          first print enough blanks, then enough symbols
              10 nRows = int(input('Enter the number of rows: '))
              11 symbol = input('Enter Symbol: ')
              12 nSpaces = 0
              13 nSymbols = 2 * nRows - 1
              14 for i in range(nRows):
              15     print(' ' * nSpaces, end = '')
              16     print(symbol * nSymbols)
              17     nSpaces = nSpaces + 1
              18     nSymbols = nSymbols - 2

                                                                                              Looping in Python  145
   154   155   156   157   158   159   160   161   162   163   164