Page 319 - Computer Science V2.0 Class 11
P. 319

17         nSpaces = nSpaces - 1
                   18         nSymbols = nSymbols + 2
                  >>> isoscelesTriangle(6,"*")

                 Sample Output:
                           *
                          ***
                         *****
                        *******
                       *********
                      ***********
                 Fig 11.7 : Isosceles Triangle

                 11.7.1 Power Drum
                 Having learnt, how to display an inverted isosceles triangle (program 11.13) and an isosceles triangle (Program 11.14),
                 to display a power drum we just need to invoke these functions. For example, to display a power drum of asterisks
                 comprising 12 rows, we need to display an inverted isosceles triangle, followed by an isosceles triangle, each comprising
                 six rows. Note that the number of rows in a power drum is always even.

                 Program 11.15 Write a function powerDrum(nRows,symbol) to display the power drum pattern of a symbol.

                   01 def powerDrum(nRows, symbol):
                   02     '''
                   03     Objective: To display an power drum pattern
                   04     Inputs:
                   05       nRows : number of rows
                   06       symbol: symbol to be used in the pattern
                   07     Return value: None
                   08     '''
                   09     #Approach: Ensure nRows is even
                   10     #Invoke functions invertedIsoTriangle,isoscelesTriangle
                   11     assert nRows%2==0, 'A power drum must have even no of rows'
                   12     invertedIsoTriangle(nRows//2, symbol)
                   13     isoscelesTriangle(nRows//2, symbol)
                  >>> powerDrum(12,'*')
                 Sample Output:

                      ***********
                       *********
                        *******
                         *****
                          ***
                           *
                           *
                          ***
                         *****
                        *******
                       *********
                      ***********
                 Fig 11.8: Power Drum
                 11.7.2 Diamond
                 To  display  a  diamond,  it  would  be  tempting  to  invoke  the  functions  isoscelesTriangle() and
                 invertedIsoTriangle().  We  first  display  the  isosceles  triangle  and  then  the  inverted  isosceles  triangle
                 (see Fig 11.9).




                                                                                                   Looping in Python  305
   314   315   316   317   318   319   320   321   322   323   324