Difference between revisions of "CSC111 lab5 Solutions"
(→Recipes) |
(→Recipes) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 56: | Line 56: | ||
def singSong( name ): | def singSong( name ): | ||
− | for i in range( | + | for i in range( 2 ): |
hbday() | hbday() | ||
dear( name ) | dear( name ) | ||
Line 83: | Line 83: | ||
def printLines( recipe ): | def printLines( recipe ): | ||
separatorLine() | separatorLine() | ||
− | + | print recipe[0] | |
− | |||
separatorLine() | separatorLine() | ||
for line in recipe[1:]: | for line in recipe[1:]: | ||
Line 110: | Line 109: | ||
==Taxi Cab== | ==Taxi Cab== | ||
+ | [[Image:taxiCabCSC111.png | right | 200px]] | ||
Here is Megan's Taxi Cab: | Here is Megan's Taxi Cab: | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
. | . | ||
Line 117: | Line 118: | ||
# 111c-au | # 111c-au | ||
# Megan Caska | # Megan Caska | ||
− | + | # Edited by D. Thiebaut | |
+ | # | ||
from graphics import * | from graphics import * | ||
− | + | ||
def main(): | def main(): | ||
win = GraphWin( "Taxi", 800,600) | win = GraphWin( "Taxi", 800,600) | ||
+ | |||
+ | # body of car | ||
rect = Rectangle(Point (225, 250), Point(575, 350)) | rect = Rectangle(Point (225, 250), Point(575, 350)) | ||
rect.setFill('yellow') | rect.setFill('yellow') | ||
rect.draw(win) | rect.draw(win) | ||
+ | |||
+ | # top of car | ||
rect2 = Rectangle(Point (300, 175), Point (500, 250)) | rect2 = Rectangle(Point (300, 175), Point (500, 250)) | ||
rect2.setFill('yellow') | rect2.setFill('yellow') | ||
rect2.draw(win) | rect2.draw(win) | ||
− | rect3 =(390, 240)) | + | |
+ | # left window | ||
+ | rect3 =Rectangle(Point(310, 180), Point( 390, 240 ) ) | ||
rect3.setFill('white') | rect3.setFill('white') | ||
rect3.draw(win) | rect3.draw(win) | ||
− | rect4 =( | + | |
+ | # right window | ||
+ | rect4 =Rectangle( Point(410, 180), Point( 492, 242) ) | ||
rect4.setFill('white') | rect4.setFill('white') | ||
rect4.draw(win) | rect4.draw(win) | ||
center = Point(400, 300) | center = Point(400, 300) | ||
+ | |||
+ | # label in middle of body of car | ||
label = Text (center, "TAXI") | label = Text (center, "TAXI") | ||
label.draw(win) | label.draw(win) | ||
+ | |||
+ | # wheels | ||
ccenter1 = Point(300, 350) | ccenter1 = Point(300, 350) | ||
ccenter2 = Point(500, 350) | ccenter2 = Point(500, 350) | ||
Line 145: | Line 159: | ||
wheel2.setFill('black') | wheel2.setFill('black') | ||
wheel2.draw(win) | wheel2.draw(win) | ||
+ | |||
+ | # keep window up until user presses Enter key | ||
raw_input( "Press Enter to continue..." ) | raw_input( "Press Enter to continue..." ) | ||
win.close() | win.close() | ||
− | + | ||
main() | main() | ||
+ | |||
. | . | ||
Line 154: | Line 171: | ||
<br /> | <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 /> |
Latest revision as of 16:33, 27 February 2010
Contents
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
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()
.
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()