Difference between revisions of "CSC111 lab5 Solutions"

From dftwiki3
Jump to: navigation, search
(Created page with ' <onlydft> ==Recipes== <source lang="python"> # recipe.py # def separatorLine(): print 30 * '-' + 'oOo' + 30 * '-' def printLines( recipe ): separatorLine() f…')
 
(Recipes)
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
  
 +
==Stick Figure==
 +
<source lang="python">
 +
 +
# newstick.py
 +
# Draws sticks on the screen
 +
 +
def head():
 +
    print
 +
    print "  o    "
 +
 +
def body():
 +
    print "  /|\\  "
 +
 +
def legs():
 +
    print " _/ \\_  "
 +
    print
 +
 +
def roundBody():
 +
    print "  /O\\    "
 +
 +
def longLegs():
 +
    print "  / \\  "
 +
    print "_/  \\_ "
 +
    print
 +
 +
def skinnyStickFigure():
 +
    head();
 +
    body();
 +
    legs();
 +
 +
def roundLongFigure():
 +
    head();
 +
    roundBody()
 +
    longLegs()
 +
 +
def main():
 +
    skinnyStickFigure()
 +
    roundLongFigure()
 +
main()
 +
 +
</source>
 +
 +
==Birthday==
 +
 +
<source lang="python">
 +
# happyBirthday.py
 +
# D. Thiebaut
 +
 +
def hbday():
 +
    print "Happy birthday to you!"
 +
 +
def dear( name ):
 +
    print "Happy birthday, Dear", name
 +
 +
def singSong( name ):
 +
    for i in range( 2 ):
 +
        hbday()
 +
    dear( name )
 +
    hbday()
 +
    print "\n\n"
 +
 +
def main():
 +
    singSong( "Alex" )
  
<onlydft>
+
    for ch in 'abcdefghijklmnopqrstuvwxyz':
 +
        singSong( "111c-a" + ch )
 +
 
 +
main()
 +
 
 +
</source>
  
 
==Recipes==
 
==Recipes==
Line 14: Line 83:
 
def printLines( recipe ):
 
def printLines( recipe ):
 
     separatorLine()
 
     separatorLine()
     for line in recipe[0:1]:
+
     print recipe[0]
        print line
 
 
     separatorLine()
 
     separatorLine()
 
     for line in recipe[1:]:
 
     for line in recipe[1:]:
Line 40: Line 108:
 
</source>
 
</source>
  
</onlydft>
+
==Taxi Cab==
 +
[[Image:taxiCabCSC111.png | right | 200px]]
 +
Here is Megan's Taxi Cab:
 +
 
 +
<source lang="python">
 +
.
 +
 
 +
# taxi.py                                                                                                           
 +
# 111c-au                                                                                                           
 +
# Megan Caska                                                                                                       
 +
# Edited by D. Thiebaut
 +
#
 +
from graphics import *
 +
 +
def main():
 +
    win = GraphWin( "Taxi", 800,600)
 +
 
 +
    # body of car
 +
    rect = Rectangle(Point (225, 250), Point(575, 350))
 +
    rect.setFill('yellow')
 +
    rect.draw(win)
 +
 
 +
    # top of car
 +
    rect2 = Rectangle(Point (300, 175), Point (500, 250))
 +
    rect2.setFill('yellow')
 +
    rect2.draw(win)
 +
 
 +
    # left window
 +
    rect3 =Rectangle(Point(310, 180), Point( 390, 240 ) )
 +
    rect3.setFill('white')
 +
    rect3.draw(win)
 +
 
 +
    # right window
 +
    rect4 =Rectangle( Point(410, 180), Point( 492, 242) )
 +
    rect4.setFill('white')
 +
    rect4.draw(win)
 +
    center = Point(400, 300)
 +
 
 +
    # label in middle of body of car
 +
    label = Text (center, "TAXI")
 +
    label.draw(win)
 +
 
 +
    # wheels
 +
    ccenter1 = Point(300, 350)
 +
    ccenter2 = Point(500, 350)   
 +
    wheel1 = Circle(ccenter1, 50)
 +
    wheel1.setFill('black')
 +
    wheel1.draw(win)
 +
    wheel2 = Circle(ccenter2, 50)
 +
    wheel2.setFill('black')
 +
    wheel2.draw(win)
 +
 
 +
    # keep window up until user presses Enter key
 +
    raw_input( "Press Enter to continue..." )
 +
    win.close()
 +
 +
main()
 +
 +
 
 +
.
 +
</source>
 +
 +
<br />
 +
<br />
 +
[[Image:ShannonTaxi.png | 200px | right ]]
 +
And here's Shannon's
 +
 
 +
<br />
 +
<source lang="python">
 +
# taxi.py
 +
# Shannon
 +
# Edited by D. Thiebaut
 +
 
 +
from graphics import *
 +
 
 +
def main():
 +
    # create window
 +
    win = GraphWin("Taxi")
 +
 
 +
    # create all the points needed
 +
    a = Point(60, 125)
 +
    b = Point(100, 100)
 +
    c = Point(140, 125)
 +
    d = Point(60, 75)
 +
    e = Point(140, 25)
 +
    f = Point(20, 125)
 +
    g = Point(180, 75)
 +
    h = Point(62, 73)
 +
    i = Point(99, 27)
 +
    j = Point(101, 73)
 +
    k = Point(138, 27)
 +
 
 +
    # create all the rectangles
 +
    rect1 = Rectangle(d, e)
 +
    rect2 = Rectangle(f,g)
 +
    rect3 = Rectangle(h,i)
 +
    rect4 = Rectangle(j,k)
 +
 
 +
    # create the two wheels
 +
    circ1 = Circle(a, 25)
 +
    circ2 = Circle(c, 25)
 +
 
 +
    # fill with the appropriate color
 +
    rect1.setFill('yellow')
 +
    rect2.setFill('yellow')
 +
    rect3.setFill('blue')
 +
    rect4.setFill('blue')
 +
    circ1.setFill('black')
 +
    circ2.setFill('black')
 +
 
 +
    # draw them in the right order.
 +
    rect1.draw(win)
 +
    rect2.draw(win)
 +
    rect3.draw(win)
 +
    rect4.draw(win)
 +
    circ1.draw(win)
 +
    circ2.draw(win)
 +
    label = Text(b, 'taxi')
 +
    label.draw(win)
 +
 
 +
    # wait for user to close window
 +
    raw_input( "Press Enter to finish" )
 +
    win.close()
 +
 
 +
main()
 +
 
 +
</source>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
 
 +
 
 +
[[Category:CSC111]][[Category:Python]][[Category:Labs]]

Latest revision as of 15:33, 27 February 2010


Stick Figure

# newstick.py
# Draws sticks on the screen
 
def head():
    print
    print "   o     "
 
def body():
    print "  /|\\   "
 
def legs():
    print " _/ \\_  "
    print
 
def roundBody():
    print "  /O\\    " 
 
def longLegs():
    print "  / \\   "
    print "_/   \\_ "
    print
 
def skinnyStickFigure():
    head();
    body();
    legs();
 
def roundLongFigure():
    head();
    roundBody()
    longLegs()

def main():
    skinnyStickFigure()
    roundLongFigure()
main()

Birthday

# happyBirthday.py
# D. Thiebaut

def hbday():
    print "Happy birthday to you!"

def dear( name ):
    print "Happy birthday, Dear", name

def singSong( name ):
    for i in range( 2 ):
        hbday()
    dear( name )
    hbday()
    print "\n\n"

def main():
    singSong( "Alex" )

    for ch in 'abcdefghijklmnopqrstuvwxyz':
        singSong( "111c-a" + ch )

main()

Recipes

# recipe.py
# 

def separatorLine():
     print  30 * '-' + 'oOo' + 30 * '-'

def printLines( recipe ):
    separatorLine()
    print recipe[0]
    separatorLine()
    for line in recipe[1:]:
        print line
    separatorLine()
    print "\n\n"

def main():
    recipe1 = ["Smoked Salmon Tortellini with Bechamel Sauce",
               "2 packages tortellini",
               "1 bay leaf",
               "2 whole cloves",
               "1 pinch nutmeg",
               "1 chopped red bell pepper",
               "1/2 lb fresh asparagus",
               "10 ounces fresh mushrooms" ]
    recipe2 = [ "Bechamel Sauce", "1/4 cup butter", "2 tbsp flour", "1/4 cup milk"]

    printLines( recipe1 )
    printLines( recipe2 )

main()

Taxi Cab

TaxiCabCSC111.png

Here is Megan's Taxi Cab:

.

# taxi.py                                                                                                             
# 111c-au                                                                                                             
# Megan Caska                                                                                                         
# Edited by D. Thiebaut
#
from graphics import *
 
def main():
    win = GraphWin( "Taxi", 800,600)

    # body of car
    rect = Rectangle(Point (225, 250), Point(575, 350))
    rect.setFill('yellow')
    rect.draw(win)

    # top of car 
    rect2 = Rectangle(Point (300, 175), Point (500, 250))
    rect2.setFill('yellow')
    rect2.draw(win)

    # left window
    rect3 =Rectangle(Point(310, 180), Point( 390, 240 ) )
    rect3.setFill('white')
    rect3.draw(win)

    # right window
    rect4 =Rectangle( Point(410, 180), Point( 492, 242) )
    rect4.setFill('white')
    rect4.draw(win)
    center = Point(400, 300)

    # label in middle of body of car
    label = Text (center, "TAXI")
    label.draw(win)

    # wheels
    ccenter1 = Point(300, 350)
    ccenter2 = Point(500, 350)    
    wheel1 = Circle(ccenter1, 50)
    wheel1.setFill('black')
    wheel1.draw(win)
    wheel2 = Circle(ccenter2, 50)
    wheel2.setFill('black')
    wheel2.draw(win)

    # keep window up until user presses Enter key
    raw_input( "Press Enter to continue..." )
    win.close()
 
main()
 

.



ShannonTaxi.png

And here's Shannon's


# taxi.py
# Shannon
# Edited by D. Thiebaut

from graphics import *

def main():
    # create window
    win = GraphWin("Taxi")

    # create all the points needed
    a = Point(60, 125)
    b = Point(100, 100)
    c = Point(140, 125)
    d = Point(60, 75)
    e = Point(140, 25)
    f = Point(20, 125)
    g = Point(180, 75)
    h = Point(62, 73)
    i = Point(99, 27)
    j = Point(101, 73)
    k = Point(138, 27)

    # create all the rectangles
    rect1 = Rectangle(d, e)
    rect2 = Rectangle(f,g)
    rect3 = Rectangle(h,i)
    rect4 = Rectangle(j,k)

    # create the two wheels
    circ1 = Circle(a, 25)
    circ2 = Circle(c, 25)

    # fill with the appropriate color
    rect1.setFill('yellow')
    rect2.setFill('yellow')
    rect3.setFill('blue')
    rect4.setFill('blue')
    circ1.setFill('black')
    circ2.setFill('black')

    # draw them in the right order.
    rect1.draw(win)
    rect2.draw(win)
    rect3.draw(win)
    rect4.draw(win)
    circ1.draw(win)
    circ2.draw(win)
    label = Text(b, 'taxi')
    label.draw(win)

    # wait for user to close window
    raw_input( "Press Enter to finish" )
    win.close()

main()