Christmas_tree funciton

we will start from here and then make this better and better.


In [ ]:


In [18]:
'''
1. we use keyward and default argumenet below:
'''
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'

In [21]:
#we can call this function by keywors. and if one is missing, default is used.
christmas_tree(position=12,size=3)
christmas_tree(position=12)
christmas_tree()


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

In [22]:
#we can use __doc__ to print its document string
print christmas_tree.__doc__


 christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    

In [32]:
#Now we check the input
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    if int(position)!=position:
        print ":-("
        return 1
    
    if int(size)!=size:
        print ":-("
        return 1
    
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'
        
christmas_tree(8.3,6)
christmas_tree(8,2.1)
christmas_tree(-5,2)
christmas_tree(3,-14)


:-(
:-(
  *
 ***
 ***
 ***
 ***
    ***
    ***
    ***

In [33]:
#Now we check the input
#we can combine two condition by a "or"
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    if int(position)!=position or int(size)!=size:
        print ":-("
        return 1
    
    
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'
        
christmas_tree(8.3,6)
christmas_tree(8,2.1)
christmas_tree(-5,2)
christmas_tree(3,-14)


:-(
:-(
  *
 ***
 ***
 ***
 ***
    ***
    ***
    ***

In [41]:
#we check the value of position and size
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    if int(position)!=position or int(size)!=size:
        print ":-("
        return 1
    
    if position<1 or size<1:
        print ":-("
        return 1
    
    if 50<position or 50<size:
        print ":-("
        return 1
    
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'
        
christmas_tree(8.3,6)
christmas_tree(8,2.1)
christmas_tree(-5,2)
christmas_tree(3,-14)


:-(
:-(
:-(
:-(
Out[41]:
1

In [47]:
#actually we can use a built in funciton any() to get all error. 
#
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    errorlist=[int(position)!=position,int(size)!=size,position<1,size<1,50<position,50<size]
    error=any(errorlist)
    if error:
        print ":-("
        return 1
   
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'
        
christmas_tree(8.3,6)
christmas_tree(8,2.1)
christmas_tree(-5,2)
christmas_tree(3,-14)


:-(
:-(
:-(
:-(
Out[47]:
1

In [48]:
#But what if use input a string?
christmas_tree('a',-14)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-a174f56b2f02> in <module>()
      1 #But what if use input a string?
----> 2 christmas_tree('a',-14)

<ipython-input-47-a4ece0e1f934> in christmas_tree(position, size)
      8 
      9     '''
---> 10     errorlist=[int(position)!=position,int(size)!=size,position<1,size<1,50<position,50<size]
     11     error=any(errorlist)
     12     if error:

ValueError: invalid literal for int() with base 10: 'a'

In [53]:
#Acutally a powerful function is provided 
print isinstance(2, int)
print isinstance('asd', int)


True
False

In [60]:
#actually we can use a built in funciton any() to get all error. 
# and we can use 'not' to inver the result
def christmas_tree(position=8,size=6):
    ''' christmas_tree:
    print a christmas with different position and size.
    Accept two integers and then draw. If the input are not integers, print " :=(" , return 1 and exit.
    if the input is oversize,  print " :=(" , return 1 and exit.
    
    '''
    errorlist=[not isinstance(position, int),not isinstance(size, int),position<1,size<1,50<position,50<size]
    print errorlist
    error=any(errorlist)
    if error:
        print ":-("
        return 1
   
    #樹枝
    for i in range(0,size):
        print " "*position+" "* (size-i)+"*"*(2*i+1)
    #樹幹
    for i in range(0,3):
        print " "*position+" "*(size-2)+' ***'

christmas_tree(8.3,6)
christmas_tree(8,2.1)
christmas_tree(-5,2)
christmas_tree(3,-14)
christmas_tree('a',-14)


[True, False, False, False, False, False]
:-(
[False, True, False, False, False, False]
:-(
[False, False, True, False, False, False]
:-(
[False, False, False, True, False, False]
:-(
[True, False, False, True, True, False]
:-(
Out[60]:
1

In [61]:
for i in range(1,40,5):
    christmas_tree(i,i)


[False, False, False, False, False, False]
  *
  ***
  ***
  ***
[False, False, False, False, False, False]
            *
           ***
          *****
         *******
        *********
       ***********
           ***
           ***
           ***
[False, False, False, False, False, False]
                      *
                     ***
                    *****
                   *******
                  *********
                 ***********
                *************
               ***************
              *****************
             *******************
            *********************
                     ***
                     ***
                     ***
[False, False, False, False, False, False]
                                *
                               ***
                              *****
                             *******
                            *********
                           ***********
                          *************
                         ***************
                        *****************
                       *******************
                      *********************
                     ***********************
                    *************************
                   ***************************
                  *****************************
                 *******************************
                               ***
                               ***
                               ***
[False, False, False, False, False, False]
                                          *
                                         ***
                                        *****
                                       *******
                                      *********
                                     ***********
                                    *************
                                   ***************
                                  *****************
                                 *******************
                                *********************
                               ***********************
                              *************************
                             ***************************
                            *****************************
                           *******************************
                          *********************************
                         ***********************************
                        *************************************
                       ***************************************
                      *****************************************
                                         ***
                                         ***
                                         ***
[False, False, False, False, False, False]
                                                    *
                                                   ***
                                                  *****
                                                 *******
                                                *********
                                               ***********
                                              *************
                                             ***************
                                            *****************
                                           *******************
                                          *********************
                                         ***********************
                                        *************************
                                       ***************************
                                      *****************************
                                     *******************************
                                    *********************************
                                   ***********************************
                                  *************************************
                                 ***************************************
                                *****************************************
                               *******************************************
                              *********************************************
                             ***********************************************
                            *************************************************
                           ***************************************************
                                                   ***
                                                   ***
                                                   ***
[False, False, False, False, False, False]
                                                              *
                                                             ***
                                                            *****
                                                           *******
                                                          *********
                                                         ***********
                                                        *************
                                                       ***************
                                                      *****************
                                                     *******************
                                                    *********************
                                                   ***********************
                                                  *************************
                                                 ***************************
                                                *****************************
                                               *******************************
                                              *********************************
                                             ***********************************
                                            *************************************
                                           ***************************************
                                          *****************************************
                                         *******************************************
                                        *********************************************
                                       ***********************************************
                                      *************************************************
                                     ***************************************************
                                    *****************************************************
                                   *******************************************************
                                  *********************************************************
                                 ***********************************************************
                                *************************************************************
                                                             ***
                                                             ***
                                                             ***
[False, False, False, False, False, False]
                                                                        *
                                                                       ***
                                                                      *****
                                                                     *******
                                                                    *********
                                                                   ***********
                                                                  *************
                                                                 ***************
                                                                *****************
                                                               *******************
                                                              *********************
                                                             ***********************
                                                            *************************
                                                           ***************************
                                                          *****************************
                                                         *******************************
                                                        *********************************
                                                       ***********************************
                                                      *************************************
                                                     ***************************************
                                                    *****************************************
                                                   *******************************************
                                                  *********************************************
                                                 ***********************************************
                                                *************************************************
                                               ***************************************************
                                              *****************************************************
                                             *******************************************************
                                            *********************************************************
                                           ***********************************************************
                                          *************************************************************
                                         ***************************************************************
                                        *****************************************************************
                                       *******************************************************************
                                      *********************************************************************
                                     ***********************************************************************
                                                                       ***
                                                                       ***
                                                                       ***

In [ ]: