Python code to print a diamond pattern.
# import python modules import sys import os import string # Declare number of lines to be read this value should be odd line = 15 print "print %s" % (line) # Calculate centre of the diamond, also calculate the offset centre = line/2 offset = line - 2*centre if offset == 0: print "...Error the number of lines should be odd" sys.exit() if offset == 1: centre = centre + 1 # Print statements print "centre %s" %(centre) print "--Here in the diamond for lines = %s--" % (line) # Generate the diamond pattern # Calculate spaces and stars for each line for line_d in range (1,line+1,1): spaces = centre - line_d if spaces < 0: spaces = spaces * -1 stars = 2*(centre - spaces) - 1 diamond = "" for l in range (0, spaces, 1): diamond = diamond + " " for q in range (0, stars, 1): diamond = diamond + "*" print diamond