Difference between revisions of "CSC111 Solutions for Lab 2"
(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…') |
|||
(One intermediate revision by the same user not shown) | |||
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 |
− | + | 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 |
− | + | 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 | + | for i in range( 4 ): |
− | + | farm.append( raw_input( "animal? " ) ) | |
− | |||
− | # | + | # print the contents of the farm |
− | + | for animal in farm: | |
− | for | + | 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? | # How many stars? | ||
noStars = input( "How many stars? " ) | noStars = input( "How many stars? " ) | ||
Line 84: | Line 94: | ||
</pre></code> | </pre></code> | ||
+ | |||
+ | [[Category:CSC111]][[Category:Labs]] |
Latest revision as of 08:47, 5 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