Difference between revisions of "CSC111 Lab 1 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- __TOC__ =Solution programs= <br /> <source lang="python"> # some solution programs for Lab 1 # CSC111 # D. Thiebaut # (by the way, lines that start with a # sig...")
 
(Playing with semantics)
Line 91: Line 91:
  
 
<br />
 
<br />
=Playing with semantics=
+
<br />
* All these various loops print the same thing...
+
 
 
<br />
 
<br />
 
<br />
 
<br />
<source lang="python">
 
  
list = [ "Lea Jones", "Julie Fleur", "Anu Vias", "Anna Bell", "Lynn Grave" ]
+
<br />
  
 +
<center><videoflash>sHTNn04DYNY</videoflash></center>
  
#--- first loop example ---
+
<br />
for name in list:
 
    print( "+-" + "-" * len( name ) + "-+" )
 
    print( "|", name, "|" )
 
    print( "+-" + "-" * len( name ) + "-+" )
 
    print()
 
 
 
#--- separator ---
 
print( "\n" + "-"*80 + "\n" )
 
 
 
 
 
#--- second loop example ---
 
for name in list:
 
    noDashes = len( name ) + 2
 
    line    = '-' * noDashes
 
    print( '+' + line + '+')
 
    print( '|' , name , '|' )
 
    print( '+' + line + '+')
 
   
 
 
 
#--- separator ---
 
print( "\n" + "-"*80 + "\n" )
 
 
 
 
 
#--- third loop example ---
 
for name in list:
 
    noChars  = len( name )
 
    noDashes = noChars + 2
 
    line    = '+' + '-'*noDashes + '+'
 
    print( line )
 
    print( '|' , name , '|' )
 
    print( line )
 
   
 
 
 
</source>
 
  
 +
<br />
 
<br />
 
<br />
  
 
<br />
 
<br />
 
 
[[Category:CSC111]][[Category:Python]][[Category:Labs]]
 
[[Category:CSC111]][[Category:Python]][[Category:Labs]]

Revision as of 11:41, 30 January 2014

--D. Thiebaut (talk) 11:23, 30 January 2014 (EST)


Solution programs


# some solution programs for Lab 1
# CSC111
# D. Thiebaut
# (by the way, lines that start with a # sign
# are not interpreted by Python.  They are called
# comments, and we can use them to document programs!)
#

# challenge #1
a = 3
b = 10
c = 20
print( "a =", a )
print( "b =", b )
print( "c =", c )
print( "sum =", a+b+c )



# challenge #2
word1 = "hello"
word2 = "there!"
print( word1, word2 )
print( word2, word1 )
print( word1, word1, word1 )
print( word1, word2, word2, word1 )

 
# challenge #3
print( word1 * 4, word2 )
print( word1, (word2+' ') * 5+ word1 )


# challenge #4
print( (word1+word2) * 5 )


# challenge #5
list = [ "Lea Jones", "Julie Fleur", "Anu Vias", "Anna Bell", "Lynn Grave" ]
for name in list:
    print( name )


# challenge #6
list = [ "Lea Jones", "Julie Fleur", "Anu Vias", "Anna Bell", "Lynn Grave" ]
for name in list:
    print( name )
    print( "-" * len( name ) )


# challenge #7
list = [ "Lea Jones", "Julie Fleur", "Anu Vias", "Anna Bell", "Lynn Grave" ]
for name in list:
    print( "-" * len( name ) )
    print( name )
    print( "-" * len( name ) )
    print()


# challenge of the Day
list = [ "Lea Jones", "Julie Fleur", "Anu Vias", "Anna Bell", "Lynn Grave" ]
for name in list:
    print( "--" + "-" * len( name ) + "--" )
    print( "|", name, "|" )
    print( "--" + "-" * len( name ) + "--" )
    print()
    

for name in list:
    print( "+-" + "-" * len( name ) + "-+" )
    print( "|", name, "|" )
    print( "+-" + "-" * len( name ) + "-+" )
    print()