Tuesday, July 30, 2019

Draw a tree of required height

Script 

#!/usr/local/bin/python3.7

def tree(height):

    """

    Draws a tree of a given height.

    """

    row = 0

    while row < height:

        count = 0

        while count < height - row:

            print(end = " ")

            count = count + 1

        count = 0

        while count < (2*row + 1):

            print(end = "*")

            count = count + 1

        print ()

        row = row + 1


def main():

    """ Allows users to draw trees of various heights """

    height = int(input("Enter the height of the tree: "))

    tree(height)


main()


Execution


Enter the height of the tree: 25

                         *

                        ***

                       *****

                      *******

                     *********

                    ***********

                   *************

                  ***************

                 *****************

                *******************

               *********************

              ***********************

             *************************

            ***************************

           *****************************

          *******************************

         *********************************

        ***********************************

       *************************************

      ***************************************

     *****************************************

    *******************************************

   *********************************************

  ***********************************************

 *************************************************

















No comments:

Post a Comment