Difference between revisions of "CSC111 Homework 2 Solutions"
(Created page with '<onlydft> =Problem #1= <source lang="python"> # hw2b.py # 111a-am & 111a-an # Nora Youngs & Zinmar Aung # (edited by D. Thiebaut) # September 20, 2007 # # This program prompts th…') |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __TOC__ | ||
<onlydft> | <onlydft> | ||
− | =Problem #1= | + | ==Problem #1== |
<source lang="python"> | <source lang="python"> | ||
− | # | + | # hw2a.py |
− | |||
− | |||
− | |||
− | |||
− | |||
# This program prompts the user to enter a number | # This program prompts the user to enter a number | ||
# Then it will print out four different triangles | # Then it will print out four different triangles | ||
Line 87: | Line 83: | ||
main() | main() | ||
+ | </source> | ||
+ | |||
+ | ==Problem #2== | ||
+ | <source lang="python"> | ||
+ | # hw2b.py | ||
+ | # Andrea Spain and Kate Zdepski | ||
+ | # 111c-aw | ||
+ | # This program asks the user for three animals, paired with | ||
+ | # their respective noises. It then prints them in the lyrics to Old MacDonald. | ||
+ | |||
+ | def main(): | ||
+ | # Gets three animals and their noises from user. | ||
+ | # Adds them to a list of the list. | ||
+ | print "Welcome to old MacDonald's farm." | ||
+ | print "Please enter three animals and the noise they make: " | ||
+ | print "" | ||
+ | farm = [] | ||
+ | for i in range(3): | ||
+ | pair = [ raw_input( "animal? " ), raw_input( "noise? " ) ] | ||
+ | farm.append(pair) | ||
+ | |||
+ | # Separates the pair list into two variables and | ||
+ | # prints them into the song using a loop. | ||
+ | for pair in farm : | ||
+ | animal, noise = pair | ||
+ | print "" | ||
+ | print "Old MacDonald had a farm, E-I-E-I-O" | ||
+ | print "And on his farm he had some " + animal+ "s" + ", E-I-E-I-O" | ||
+ | print "And a", noise, "here, and a", noise, | ||
+ | print "there, everywhere a", noise + "!" | ||
+ | |||
+ | main() | ||
+ | |||
+ | </source> | ||
</onlydft> | </onlydft> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Homework]] |
Latest revision as of 08:43, 7 September 2011