Difference between revisions of "CSC111 Homework 2 Solutions 2011"

From dftwiki3
Jump to: navigation, search
(hw2b.py)
(hw2c.py)
 
(One intermediate revision by the same user not shown)
Line 134: Line 134:
 
</source>
 
</source>
 
<br />
 
<br />
 +
==hw2c.py==
 +
A nicely documented program with a clever way of generating the farm of animals.
 +
<br />
 +
<source lang="python">
 +
# hw2d.py
 +
# 111a-bt  Maria Laura Peraza
 +
# 111a-bn  Elena Coleman    (working in pair programming)
 +
# this program prompts the user for three animanls and their respective
 +
# sounds, and incorporates them to the song "Old MacDonald's Farm"
 +
#
 +
# Example of user interaction:
 +
# Welcome to old MacDonald's farm!
 +
# Please enter 3 animals and the noise they make:
 +
# What is the first animal? cow
 +
# What is the first animal's sound? moo
 +
# What is the second animal? hen
 +
# What is the second animal's sound? cluck
 +
# What is the third animal? sheep
 +
# What is the third animal's sound? baa
 +
#
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
# And on his farm he had some cows, E-I-E-I-O
 +
# And a moo-moo here, and a moo-moo there, everywhere a moo-moo!
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
#
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
# And on his farm he had some hens, E-I-E-I-O
 +
# And a cluck-cluck here, and a cluck-cluck there, everywhere a cluck-cluck!
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
#
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
# And on his farm he had some sheeps, E-I-E-I-O
 +
# And a baa-baa here, and a baa-baa there, everywhere a baa-baa!
 +
# Old MacDonald had a farm, E-I-E-I-O
 +
#
 +
 +
 +
def main():
 +
    # greet user
 +
    print( "Welcome to old MacDonald's farm!" )
 +
    print()
 +
 +
    # get the animals and their sound
 +
    print ("Please enter 3 animals and the noise they make:")
 +
    pair1  = [ input("What is the first animal? "), input("What is the first animal's sound? (only one word needed) ") ]
 +
    pair2  = [ input("What is the second animal? "), input("What is the second animal's sound? (only one word needed) ") ]
 +
    pair3  = [ input("What is the third animal? "), input("What is the third animal's sound? (only one word needed) ") ]
 +
    animals = [pair1, pair2, pair3]
 +
   
 +
 +
    # generate the lyrics
 +
    for name, sound in animals:
 +
        print()
 +
        sound = sound + "-" + sound
 +
        print( "Old MacDonald had a farm, E-I-E-I-O")
 +
        print( "And on his farm he had some", name + "s, E-I-E-I-O" )
 +
        print( "And a " + sound + " here, and a " + sound +
 +
                " there, everywhere a " + sound + "!" )
 +
        print( "Old MacDonald had a farm, E-I-E-I-O" )
 +
 +
 +
main()
 +
 +
</source>
 +
 +
<br />
 +
And another version:
 +
<br />
 +
<source lang="python">
 +
# hw2d.py
 +
# 111a-bx Yingtong Ding
 +
# 111a-by Jenny Wang
 +
# Edited by D. Thiebaut
 +
#
 +
# This program allows the user to input the
 +
# name of a farm animal and the noise it makes
 +
# then, the animal and its noise become incorporated
 +
# into the lyrics of "Old MacDonald"
 +
#
 +
 +
def main():
 +
    # create an empty farm
 +
    farm = []
 +
 +
    # input 3 animals and noises and add them to the farm
 +
    for i in range(3):
 +
        animal = input( "animal: " )
 +
        noise = input ( "noise? " )
 +
        farm.append( [animal,noise ] )
 +
 +
 +
    # output lyrics and interject a different animal and sound every time
 +
    for farmAnimal, noise in farm:
 +
        print( "Old MacDonald had a farm, E-I-E-I-O" )
 +
        print( "And on his farm he had some", farmAnimal+"s"+",", "E-I-E-I-O" )
 +
        print( "And a", noise, "here, and a", noise, "there, everywhere a", noise+"!" )
 +
        print()
 +
 +
main()
 +
 +
</source>
 +
 +
 
[[Category:CSC111]][[Category:Python]][[Category:Homework]]
 
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Latest revision as of 12:51, 3 October 2011

--D. Thiebaut 13:36, 3 October 2011 (EDT)


Solution Programs

hw2b.py

# hw2b.py
# 111a-bm  Ashleigh King
# 111a-an  Sam Scharr   (working in pair programming)
#
# This program prompts the user for a number and displays
# a number of lines corresponding to that number.
#
# Example of user interaction:
# How many lines do you want printed? 3
# *
# *... **
# **...... -***
# 

def main():
    lines = input( "How many lines do you want printed? " )
    lines = eval( lines )
    for x in range( 0, lines ):
        print( "*" * (x) + "." * (x * 3) + " " + "-" * (x-1) + "*" * (x + 1) )

main()



or this version, also good:

# hw2b.py
# 111a-au Gemma Natori
# Edited by D. Thiebaut
# This program prompts the user for a number and displays 
# a number of lines corresponding to that number.
# Example of user interaction:
# How many lines do you want printed? 3
# *
# *... **
# **...... -***


def main():
    noLines = eval (input ("How many lines do you want printed? "))

    for x in range ( noLines ):
        stars = x
        dots = x * 3
        stars2 = x + 1
        dashes = x - 1
        
        print (stars * "*" + dots * "." + " " + dashes * "-" + stars2 * "*")


main()

hw2c.py

# hw2c.py
# 111ai JeeHee Oh
# this program prompts the user for a number and displays
# a number of lines corresponding to that number.
#
# Example of user interaction:
# Enter a positive number less than 20: 2
#  *
# ***
#  *
#

def main():
    
    num = eval( input ("Enter a positive number less than 20: "))
    print()

    for i in range (num):
        noSpaces = num - i
        noStars   = 2 * i + 1
        print( noSpaces * ' '  +   noStars * '*' )
    
    for a in range (num - 1, 0, -1):
        noSpaces = num - a + 1
        noStars  = 2 * a - 1
        print( noSpaces * ' '  +  noStars * '*' )
    
main()


This version is a bit more concise, but works well, too:

#hw2c.py
#Ariana DasGupta 111a-ay
#Emily Glickman 111a-ae

# Program asks used for a positive number
# Program the displays a diamond that has input*2-1
# ex. input = 5
#output =
#    *    
#   ***   
#  *****  
# ******* 
#*********
# ******* 
#  *****  
#   ***   
#    *    



def main ():
    line = eval(input('Enter a postive number less than 20: \n'))
    for i in range (line-1,0, -1):
        print (" "*(i),"*"*(line-i-1),"*","*"*(line-i-1)," "*(i), sep = '' )


    for i in range (line-1,-1, -1):
        print (" "*(line-i-1),"*"*(i),"*","*"*(i)," "*(line-i-1), sep = '' )
main ()


hw2c.py

A nicely documented program with a clever way of generating the farm of animals.

# hw2d.py
# 111a-bt   Maria Laura Peraza
# 111a-bn   Elena Coleman    (working in pair programming)
# this program prompts the user for three animanls and their respective
# sounds, and incorporates them to the song "Old MacDonald's Farm"
#
# Example of user interaction:
# Welcome to old MacDonald's farm!
# Please enter 3 animals and the noise they make:
# What is the first animal? cow
# What is the first animal's sound? moo
# What is the second animal? hen
# What is the second animal's sound? cluck
# What is the third animal? sheep
# What is the third animal's sound? baa
#
# Old MacDonald had a farm, E-I-E-I-O
# And on his farm he had some cows, E-I-E-I-O
# And a moo-moo here, and a moo-moo there, everywhere a moo-moo!
# Old MacDonald had a farm, E-I-E-I-O
#
# Old MacDonald had a farm, E-I-E-I-O
# And on his farm he had some hens, E-I-E-I-O
# And a cluck-cluck here, and a cluck-cluck there, everywhere a cluck-cluck!
# Old MacDonald had a farm, E-I-E-I-O
#
# Old MacDonald had a farm, E-I-E-I-O
# And on his farm he had some sheeps, E-I-E-I-O
# And a baa-baa here, and a baa-baa there, everywhere a baa-baa!
# Old MacDonald had a farm, E-I-E-I-O
#


def main():
    # greet user
    print( "Welcome to old MacDonald's farm!" )
    print()

    # get the animals and their sound
    print ("Please enter 3 animals and the noise they make:")
    pair1   = [ input("What is the first animal? "), input("What is the first animal's sound? (only one word needed) ") ]
    pair2   = [ input("What is the second animal? "), input("What is the second animal's sound? (only one word needed) ") ]
    pair3   = [ input("What is the third animal? "), input("What is the third animal's sound? (only one word needed) ") ]
    animals = [pair1, pair2, pair3]
    
 
    # generate the lyrics
    for name, sound in animals:
        print()
        sound = sound + "-" + sound
        print( "Old MacDonald had a farm, E-I-E-I-O")
        print( "And on his farm he had some", name + "s, E-I-E-I-O" )
        print( "And a " + sound + " here, and a " + sound + 
                " there, everywhere a " + sound + "!" )
        print( "Old MacDonald had a farm, E-I-E-I-O" )


main()


And another version:

# hw2d.py
# 111a-bx Yingtong Ding
# 111a-by Jenny Wang
# Edited by D. Thiebaut
#
# This program allows the user to input the
# name of a farm animal and the noise it makes
# then, the animal and its noise become incorporated
# into the lyrics of "Old MacDonald"
#

def main():
    # create an empty farm
    farm = []

    # input 3 animals and noises and add them to the farm
    for i in range(3):
        animal = input( "animal: " )
        noise = input ( "noise? " ) 
        farm.append( [animal,noise ] )


    # output lyrics and interject a different animal and sound every time
    for farmAnimal, noise in farm:
        print( "Old MacDonald had a farm, E-I-E-I-O" )
        print( "And on his farm he had some", farmAnimal+"s"+",", "E-I-E-I-O" )
        print( "And a", noise, "here, and a", noise, "there, everywhere a", noise+"!" )
        print()

main()