CSH3.4 MrG 2012.1009 More about functions

3520 days ago by CSH2012

#1) def concat(s1,s2): return s1+" "+s2 concat("bob","ross") 
       
'bob ross'
'bob ross'
#2) def printHouse(): print " /\\ \n/__\\\n| |\n|__|" printHouse() 
       
 /\ 
/__\
|  |
|__|
 /\ 
/__\
|  |
|__|
#3) def rightJustify(s,w): return (w-len(s))*" "+s rightJustify("batman rules",20) 
       
'        batman rules'
'        batman rules'
#4) def print10Stars(): print(10*"*") print(print10Stars()) 
       
**********
None
**********
None
def make10Stars(): return 10*"*" print(make10Stars()) 
       
**********
**********
#5) def printTriangle(n,ch): while n>0: print(n*ch) n=n-1 return 1 printTriangle(2,"*")+printTriangle(3,"#")+printTriangle(5,"y") 
       
**
*
###
##
#
yyyyy
yyyy
yyy
yy
y
3
**
*
###
##
#
yyyyy
yyyy
yyy
yy
y
3
#6) def sum1ToN(n): if n>0: return n*(n+1)//2 print(sum1ToN(6)) print(sum1ToN(-2)) 
       
21
None
21
None
#8) len(0) 
       
Traceback (click to the left of this block for traceback)
...
TypeError: object of type 'sage.rings.integer.Integer' has no len()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_19.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IzgpCmxlbigwKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmplptOvB/___code___.py", line 3, in <module>
    exec compile(u'len(_sage_const_0 )
  File "", line 1, in <module>
    
TypeError: object of type 'sage.rings.integer.Integer' has no len()
len('0') 
       
1
1
len('''0''') 
       
1
1
len('''''') 
       
0
0
len([0]) 
       
1
1
len([]) 
       
0
0
len(range(5)) 
       
5
5
range(5) 
       
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]