Python Forum

Full Version: Ascii Tree Art
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm trying to create a function that will make the correct sized ascii Christmas tree art based on the size passed in. This is what I have so far

def get_festive(size):
    Triangle = ""
    for i in range (size+4):
        Triangle += ("*"*((i*4)+1)).center(((size*4))," ")
        Triangle += "\n"
    return Triangle
This is what I need it to look like:
get_festive(3)

          *
        *****
      *********
    *************
        *****
      *********
    *************
  *****************
      *********
    *************
  *****************
********************* 
This is what I have so far:
get_festive(3)

     *      
   *****    
 *********  
*************
*****************
*********************
*************************
Any suggestions or if you know what to fix let me know - thank you!
One thing is that you need to center based on the largest line you are going to print. In the case of your current code that would be (size + 4) * 4, not just size * 4.

Beyond that it's hard to say, because the general case is not clear. Do you have more detailed instructions for the assignment, or examples of what get_festive(2) and get_festive(4) should look like?
get_festive(2)

        *
      *****
    *********
  *************
      *****
    *********
  *************
*****************
get_festive(1)

      *
    *****
  *********
*************
here are the other ones (:
You need two nested loops. Each tree is made of a number of trapezoids equal to it's size. Each trapezoid has four rows. That should tell you what the two loops you need are. The number of *'s at any level is a function of the sum of the two looping variables. Remember to center based on the largest number of *'s you will get.
ichabod801 Wrote:You need two nested loops.
I'm using only one loop
for n in range(4 * size):
    ...
The divmod() function helps.
Well, you could do it with no loops, but let's start him out with the two loop solution.