Difference between revisions of "CSC111 Solutions for Lab 2"

From dftwiki3
Jump to: navigation, search
(Created page with '<code><pre> # all numbers between 3 and 7 for i in range( 3, 8 ): print i # same thing, without the range function for i in [3, 4, 5, 6, 7, 8]: print i # all even n…')
 
Line 1: Line 1:
 
<code><pre>
 
<code><pre>
 +
# ----------------------------------------------------------
 
# all numbers between 3 and 7
 
# all numbers between 3 and 7
 
for i in range( 3, 8 ):
 
for i in range( 3, 8 ):
Line 25: Line 26:
  
  
 +
# ----------------------------------------------------------
 
# where should I start?
 
# where should I start?
 
start = input( "Where should I start? " )
 
start = input( "Where should I start? " )
Line 33: Line 35:
  
  
 +
# ----------------------------------------------------------
 
# lab2seq.py
 
# lab2seq.py
 
#
 
#
# initialize the farm with animals
+
def main():
farm = [ "dog", "cat", "horse", "pig" ]
+
    # initialize the farm with animals
 
+
    farm = [ "dog", "cat", "horse", "pig" ]
# print the contents of the farm
+
   
for animal in farm:
+
    # print the contents of the farm
    print animal
+
    for animal in farm:
 +
        print animal
  
 +
main()
  
  
 
                  
 
                  
 +
# ----------------------------------------------------------
 
# lab2seq2.py
 
# lab2seq2.py
 
#
 
#
farm = []              # create an empty farm
+
def main():
for i in range( 4 ):
+
    farm = []              # create an empty farm
    farm.append( raw_input( "animal? " ) )
+
    for i in range( 4 ):
 +
        farm.append( raw_input( "animal? " ) )
 +
 
 +
    # print the contents of the farm
 +
    counter = 1
 +
    for animal in farm:
 +
        print "animal #", counter, ": a", animal
 +
        counter = counter + 1
  
# print the contents of the farm
+
    # old MacDonald...   
counter = 1
+
    farm = []              # create an empty farm
for animal in farm:
+
    for i in range( 4 ):
    print "animal #", counter, ": a", animal
+
        farm.append( raw_input( "animal? " ) )
    counter = counter + 1
 
  
# old MacDonald...   
+
    # print the contents of the farm
farm = []              # create an empty farm
+
    for animal in farm:
for i in range( 4 ):
+
        print "Old MacDonald had a farm, E-I-E-I-O"
    farm.append( raw_input( "animal? " ) )
+
        print "And on his farm he had a", animal, ", E-I-E-I-O"
 +
        print "Here a", animal, "there a", animal, "everywhere a", animal, "!"
 +
        print
  
# print the contents of the farm
+
main()
for animal in farm:
 
    print "Old MacDonald had a farm, E-I-E-I-O"
 
    print "And on his farm he had a", animal, ", E-I-E-I-O"
 
    print "Here a", animal, "there a", animal, "everywhere a", animal, "!"
 
    print
 
  
  
 +
# ----------------------------------------------------------
 
#  How many stars?
 
#  How many stars?
 
noStars = input( "How many stars? " )
 
noStars = input( "How many stars? " )

Revision as of 12:06, 3 February 2010

# ----------------------------------------------------------
# all numbers between 3 and 7
for i in range( 3, 8 ):
    print i


# same thing, without the range function
for i in [3, 4, 5, 6, 7, 8]:
    print i




# all even numbers in (4, 20)
for i in range( 4, 21, 2 ):
    print i




# all odd numbers in (-3, 19)
for i in range( -3, 20, 2 ):
    print i



# ----------------------------------------------------------
# where should I start?
start = input( "Where should I start? " )
end   = input( "What is the last number of the series? " )
for i in range( start, end+1 ):
    print i



# ----------------------------------------------------------
# lab2seq.py
#
def main():
    # initialize the farm with animals
    farm = [ "dog", "cat", "horse", "pig" ]
    
    # print the contents of the farm
    for animal in farm:
        print animal

main()


                
# ----------------------------------------------------------
# lab2seq2.py
#
def main():
    farm = []              # create an empty farm
    for i in range( 4 ):
        farm.append( raw_input( "animal? " ) )

    # print the contents of the farm
    counter = 1
    for animal in farm:
        print "animal #", counter, ": a", animal
        counter = counter + 1

    # old MacDonald...    
    farm = []              # create an empty farm
    for i in range( 4 ):
        farm.append( raw_input( "animal? " ) )

    # print the contents of the farm
    for animal in farm:
        print "Old MacDonald had a farm, E-I-E-I-O"
        print "And on his farm he had a", animal, ", E-I-E-I-O"
        print "Here a", animal, "there a", animal, "everywhere a", animal, "!"
        print

main()


# ----------------------------------------------------------
#  How many stars?
noStars = input( "How many stars? " )

for i in range( noStars ):
    print "*",     # the comma forces the output to stay on the same line
    
for i in range( 20-noStars ):
    print "!",
    
print          # go to next line