CSC111 Solutions for Lab 2

From dftwiki3
Revision as of 12:03, 3 February 2010 by Thiebaut (talk | contribs) (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…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
# 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
#
# initialize the farm with animals
farm = [ "dog", "cat", "horse", "pig" ]

# print the contents of the farm
for animal in farm:
    print animal



                
# lab2seq2.py
#
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


#  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