Page 161 - Information_Practice_Fliipbook_Class11
P. 161
th
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):
28 print(' ' * (nInverted-i+1), end = '')
29
30 for j in range(1, 2*i):
31 if j==1 or j==2*i-1:
32 print(symbol, end = '')
33 else:
34 print(' ', end = '')
35 print()
Sample Output:
>>> Enter the number of rows (odd number): 11
>>> Enter Symbol: *
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
6.7.4 Partial Dry Run
01 i = 1
02 print 6-1=5 spaces
Looping in Python 147

