Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ascii Tree Art
#1
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!
Reply
#2
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
get_festive(2)

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

      *
    *****
  *********
*************
here are the other ones (:
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
ichabod801 Wrote:You need two nested loops.
I'm using only one loop
for n in range(4 * size):
    ...
The divmod() function helps.
Reply
#6
Well, you could do it with no loops, but let's start him out with the two loop solution.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020