简体   繁体   中英

Printing christmas tree in python

How can I make a perfect Christmas tree in python? i have here my code but its not working well, it needs to print '~~' in the first for loop.

height = 7
for a in range(1, (height + height) - 3):
    if a % 2 != 0:
        if a == 1:
            print(a * 'o')
        else:
            print(a * '* ')
for a in range(height + 1):
    if a % (height + 1) == 1:
        test = height - 3
        print(test * '~~' + a * '|' + test * '~~')
    if a % (height + 1) == 1:
        test = height - 3
        print(test * '~~' + a * '|' + test * '~~')

Output of my code is:

o
* * * 
* * * * * 
* * * * * * * 
* * * * * * * * * 
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~

my desired output is:

~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
def values():
    yield 'o'
    for i in range(3, 10, 2):
        yield ' '.join('*' * i)
    yield from '||'

for v in values():
    print('{:~^17}'.format(v))

Prints:

~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~

Or:

for v in ['o'] + [' '.join('*' * i) for i in range(3, 10, 2)] + ['|', '|']:
    print('{:~^17}'.format(v))

Here is an example using the string method center .

for x in range(1, 30, 2):
    s = '*' * x
    print(s.center(30))

              *               
             ***              
            *****             
           *******            
          *********           
         ***********          
        *************         
       ***************        
      *****************       
     *******************      
    *********************     
   ***********************    
  *************************   
 ***************************  
***************************** 
>>>

EDIT: Including the tildes.

limit = 30

for x in range(1, limit, 2):
    tildes = '~' * ((limit-x)//2)
    out = tildes + '*' * x + tildes
    print(out)

~~~~~~~~~~~~~~*~~~~~~~~~~~~~~
~~~~~~~~~~~~~***~~~~~~~~~~~~~
~~~~~~~~~~~~*****~~~~~~~~~~~~
~~~~~~~~~~~*******~~~~~~~~~~~
~~~~~~~~~~*********~~~~~~~~~~
~~~~~~~~~***********~~~~~~~~~
~~~~~~~~*************~~~~~~~~
~~~~~~~***************~~~~~~~
~~~~~~*****************~~~~~~
~~~~~*******************~~~~~
~~~~*********************~~~~
~~~***********************~~~
~~*************************~~
~***************************~
*****************************

I edited your code just a little:

height = 7
width = 17
for a in range(1, (height + height) - 3):
    if a % 2 != 0:
        if a == 1:
            sym = 'o'
            curly = ''.join(['~' for i in range((width-len(sym))//2)])
            print(curly +  a * sym + curly)
        else:
            sym = a * '* '
            curly = ''.join(['~' for i in range((width - len(sym)) // 2)])
            print(curly + sym + curly)
for a in range(height + 1):
    if a % (height + 1) == 1:
        test = height - 3
        print(test * '~~' + a * '|' + test * '~~')
    if a % (height + 1) == 1:
        test = height - 3
        print(test * '~~' + a * '|' + test * '~~')
~~~~~~~~o~~~~~~~~
~~~~~* * * ~~~~~
~~~* * * * * ~~~
~* * * * * * * ~
* * * * * * * * * 
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~

It's not perfect but no Christmas tree is 100% straight.

I have modified your code a little bit, well here's the code:

height = 7
z = height - 3
x = 1
for i in range(1, (height + height) - 3):
    if i % 2 != 0:
       if(i==1):
          print('~~' * z + 'o' +'~~' * z)
       else:
          print('~~' * z + '* ' * (x-1)+ '*' *1 +'~~' * z)
       x+=2
       z-=1
for a in range(height + 1):
    if a % (height + 1) == 1:
       test = height - 3
       print(test * '~~' + a * '|' + test * '~~')
    if a % (height + 1) == 1:
       test = height - 3
       print(test * '~~' + a * '|' + test * '~~')

Output:

~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~

It is a simple programming problem. So let's create a simple program for solving it.

First, we need to draw our tree. in each line, our tree has an odd number of starts for showing tree and some ~ characters for representing the background. If we want to have a tree which its height is equal to the height variable. at the height th line, we don't want to have any ~ character. But in the first line, we want to have only one * character.

So it seems to be a good idea to having a for loop that counts from height to the zero and relates the number of ~ characters somehow to it.

Let's see the code first:

for i in range(height, 0, -1):
    print("~" * (i - 1))    // repeating `~` character (i - 1) times

If you run this code, you will get the following output:

~~~~~~
~~~~~
~~~~
~~~
~~
~

Now let's add the * characters for drawing the tree. We should have the exact opposite relation for * characters from ~ .

for i in range(height, 0, -1):
    print("~" * (i - 1), end="")
    print("*" * (((height - i) *2) + 1))

We add end="" at the end of the previous print function. Because print will add a newline character at the end in each call and we want to prevent it (because we want to add * characters after ~ characters).

Then we want to add * characters at each line. But we want to count their number for 1 to height * 2 . But we only need the odd numbers. That's what we did in the second line. Now, we have the following number of * characters at each line: 1, 3, 5, 7,...

Below is the output of that code (We considered that height = 7 ):

~~~~~~*
~~~~~***
~~~~*****
~~~*******
~~*********
~***********
*************

As you can see, now we have our Christmas tree. But our background is incomplete. The only thing we should do is to repeat our first print statement. But this time we should not add end="" because we want that newline at the end of each line.

for i in range(height, 0, -1):
    print("~" * (i - 1), end="")
    print("*" * (((height - i) *2) + 1), end="")
    print("~" * (i - 1))

Now our tree is complete:

~~~~~~*~~~~~~
~~~~~***~~~~~
~~~~*****~~~~
~~~*******~~~
~~*********~~
~***********~
*************

You can add other things to your tree by following this simple approach. I hope this helps you.

Try using more conditionals to handle the number of stars (*). Drop a comment below if that doesn't work.

Like, you could do:

if '1star' do this..: elif '2stars' do this..:

I would code it like this:

sky=8
tree=1
for i in range(0,5):
    if i==0:
        print(sky*'~'+tree*'o'+sky*'~')
    else:
        print(sky*'~'+tree*'*'+sky*'~')
    sky-=1
    tree+=2
sky=8
tree=1
for i in range(0,2):
    print(sky*'~'+tree*'|'+sky*'~')

I modified the Christmas tree so that it looked thinner.

This is my code:

var = 14
for i in range(1, var + 1):
    print(" " * (var - i) + "*" * i + "*" * (i - 1))

Sample output:

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




The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM