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…') |
|||
Line 1: | Line 1: | ||
− | + | __TOC__ | |
− | =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> |
Revision as of 12:26, 18 February 2010
Contents
Problem #1
# hw2a.py
# This program prompts the user to enter a number
# Then it will print out four different triangles
# with that number of lines of stars
#
# typical interaction with user:
# How many lines? 3
#
# triangle 1
#
# *
# **
# ***
#
# triangle 2
#
# ***
# **
# *
#
# triangle 3
#
# *
# **
# ***
#
# triangle 4
#
# ***
# **
# *
def main():
#--------------------------------------
# input from user for number of lines of stars
# definitions for stars and spaces
#-------------------------------------
nolines = input ( " How many lines? " )
stars = "*"
space = " "
print
#---------------------------------------
# triangle 1 output
#--------------------------------------
print "triangle 1"
print
for i in range (1, nolines+1):
print i * stars
#---------------------------------------
# triangle 2 output
#---------------------------------------
print
print "triangle 2"
print
for i in range (nolines):
print (nolines-i)* stars
#-------------------------------------
# triangle 3 output
#------------------------------------
print
print "triangle 3"
print
for i in range (1, nolines+1):
print (nolines-i) * space,
print i * stars
#--------------------------------------
# triangle 4 output
#--------------------------------------
print
print "triangle 4"
print
for i in range ( nolines):
print i * space,
print (nolines-i) * stars
main()
Problem #2
# 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()