Difference between revisions of "CSC111 Lab 7 2011"
(→Challenge 5) |
|||
Line 180: | Line 180: | ||
def head(): | def head(): | ||
print | print | ||
− | print " o " | + | print( " o " ) |
def body(): | def body(): | ||
− | print " /|\\ " | + | print( " /|\\ " ) |
def legs(): | def legs(): | ||
− | print " _/ \\_ " | + | print( " _/ \\_ " ) |
print | print | ||
def roundBody(): | def roundBody(): | ||
− | print " /O\\ " | + | print( " /O\\ " ) |
def longLegs(): | def longLegs(): | ||
− | print " / \\ " | + | print( " / \\ " ) |
− | print "_/ \\_ " | + | print( "_/ \\_ " ) |
Line 209: | Line 209: | ||
<br /> | <br /> | ||
− | ; | + | <br /> |
− | : Modify the definition of the main() function only, and, this time, make it display a stick figure with a "round body" (which uses an O for the torso of the stick figure). '''<font color="magenta">Remember, you can only modify the main function. You cannot modify any of the functions other than main().</font>''' | + | {| style="width:100%; background:silver" |
+ | |- | ||
+ | | | ||
+ | ===Challenge 6=== | ||
+ | |} | ||
+ | [[Image:QuestionMark6.jpg|right|120px]] | ||
+ | <br /> | ||
+ | |||
+ | * Modify the definition of the main() function only, and, this time, make it display a stick figure with a "round body" (which uses an O for the torso of the stick figure). '''<font color="magenta">Remember, you can only modify the main function. You cannot modify any of the functions other than main().</font>''' | ||
<br /> | <br /> | ||
− | ; | + | <br /> |
− | :Similar question: make the program draw a stick figure with a "round body" and "long legs." Again, you can only change the definition of the main function. | + | <br /> |
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ===Challenge 7=== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Similar question: make the program draw a stick figure with a "round body" and "long legs." Again, you can only change the definition of the main function. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ===Challenge 8=== | ||
+ | |} | ||
+ | [[Image:QuestionMark3.jpg|right|120px]] | ||
+ | <br /> | ||
* Now, something a bit more complicated: create a new function called skinnyStickFigure() that will not contain a single print statement, but that will call other functions to display a skinny short stick figure (with a thin body and short legs). Your program should look something like this: | * Now, something a bit more complicated: create a new function called skinnyStickFigure() that will not contain a single print statement, but that will call other functions to display a skinny short stick figure (with a thin body and short legs). Your program should look something like this: | ||
<br /> | <br /> | ||
Line 224: | Line 251: | ||
def head(): | def head(): | ||
− | print | + | print() |
− | print " o " | + | print( " o " ) |
def body(): | def body(): | ||
− | print " /|\\ " | + | print( " /|\\ " ) |
def legs(): | def legs(): | ||
− | print " _/ \\_ " | + | print( " _/ \\_ " ) |
print | print | ||
def roundBody(): | def roundBody(): | ||
− | print " /O\\ " | + | print( " /O\\ " ) |
def longLegs(): | def longLegs(): | ||
− | print " / \\ " | + | print( " / \\ " ) |
− | print "_/ \\_ " | + | print( "_/ \\_ " ) |
− | print | + | print() |
def skinnyStickFigure(): | def skinnyStickFigure(): | ||
Line 257: | Line 284: | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
Line 273: | Line 291: | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Printing Recipes== | ==Printing Recipes== | ||
Line 310: | Line 302: | ||
def printLines( recipe ): | def printLines( recipe ): | ||
for line in recipe: | for line in recipe: | ||
− | print line | + | print( line ) |
Line 339: | Line 331: | ||
def separatorLine(): | def separatorLine(): | ||
− | print 30 * '-' + 'oOo' + 30 * '-' | + | print( 30 * '-' + 'oOo' + 30 * '-' ) |
. | . |
Revision as of 12:22, 19 October 2011
--D. Thiebaut 10:37, 19 October 2011 (EDT)
Contents
Testing with the if statement
A great reference for this part of the lab is the section on the if statement on Python.org. Take a quick look at it to see what it describes so that you will know where to go while working on this lab.
- Create a program with the following code:
def main():
print( "Please enter 2 numbers:" )
x = eval( input( "> " ) )
y = eval( input( "> " ) )
print( x, "/", y, "=", x/y )
main()
- Run the program a few times and check that it returns the correct information.
- Run it again and enter 0 for the second value. See what happens.
- Make your program robust by using an if-statement:
def main():
print( "Please enter 2 numbers:" )
x = eval( input( "> " ) )
y = eval( input( "> " ) )
if ( y != 0 ):
print( x, "/", y, "=", x/y )
else:
print( "Illegal value for y: division by 0 is not allowed or defined" )
main()
- Try it now! Verify that your program is now robust, i.e. you cannot make it crash (at least if you are entering numbers and not random strings!)
Challenge 1 |
- Using the above code as example, modify the program so that it asks for two numbers and prints out which number is the smallest and which is the largest.
Challenge 2 |
- Modify your code for the previous challenge and remove the two eval functions. This way your two variables will contain strings, and not numbers. Do not change the remaining part of your program and run it.
- Question 1
- What does your program do? What does it detect in the two strings you enter. For example if you enter "chocolate" and "milk" as the two strings, which will it report as the smallest? Why? What about "chocolate" and "MILK"? Why?
- Question 2
- Think of a way to make your program report that "chocolate" is lower than "milk", and that "chocolate" is also lower than "MILK"
Tests and Graphics
- Create the following program and run it.
from graphics import *
import time
def main():
w = 800
h = 600
win = GraphWin( "lab 7", w, h )
c = Circle( Point( w/2, h/2 ), 20 )
c.setFill( "magenta" )
c.draw( win )
dirX = 5 # speed in the horizontal direction
for step in range( 1000 ):
c.move( dirX, 1 )
# uncomment next line if ball moves too fast
# time.sleep( 0.05 ) # 5/100th sec.
if not ( 0 < c.getCenter().getX() < w ):
dirX = -dirX
Text( Point( w//2, h//2 ), "Click me to quit!" ).draw( win )
win.getMouse()
main()
- Notice how the ball moves. dirX is the amount of pixels it goes to the right (if dirX is positive), and 1 is the amount of pixels it goes down.
- The if statement says "if the X coordinate of the center of the circle is not between 0 and w, then change the horizontal direction." Changing the direction here means simply changing its sign. If dirX is +5, that means the ball is going to the right, then making it -5 will make the ball go to the left. If dirX is -5, then changing it to +5 means that the ball was going left, and then will go right.
Challenge 3 |
- Make the ball bounce off the horizontal sides of the graphics window
Challenge 4 |
- Make the ball change color when it crosses the middle of the graphics window. In other words, make it be, say, yellow, in the left half of the window, and red in the right half of the window.
Challenge 5 |
- You will notice that the ball bounces off the "wall" when it is already half way through it. Modify your code so that the ball bounces off the side of the window when it "touches" the side.
Functions and Stick Figures
Cut and paste the following code into a python program.
# stick.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( "_/ \\_ " )
def main():
head()
body()
legs()
main()
- Run your program. Verify that it displays a stick figure.
Challenge 6 |
- Modify the definition of the main() function only, and, this time, make it display a stick figure with a "round body" (which uses an O for the torso of the stick figure). Remember, you can only modify the main function. You cannot modify any of the functions other than main().
Challenge 7 |
- Similar question: make the program draw a stick figure with a "round body" and "long legs." Again, you can only change the definition of the main function.
Challenge 8 |
- Now, something a bit more complicated: create a new function called skinnyStickFigure() that will not contain a single print statement, but that will call other functions to display a skinny short stick figure (with a thin body and short legs). Your program should look something like this:
# 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():
#add your code here
def main():
skinnyStickFigure()
main()
- Verify that it works.
Printing Recipes
- Start with the following program:
recipe.py
#
def printLines( recipe ):
for line in recipe:
print( line )
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" ]
printLines( recipe1 )
main()
- Run the program and verify that it prints the recipe.
- Modify the program and add the following function to it (before the function printLines). Call separatorLine() once in your main program to see how it works.
.
def separatorLine():
print( 30 * '-' + 'oOo' + 30 * '-' )
.
- Question 6
- Modify the function printLines() so that it prints the recipe that it receives as shown below. (Hints: remember that you know how to take slices out of lists!)
------------------------------oOo------------------------------ Smoked Salmon Tortellini with Bechamel Sauce ------------------------------oOo------------------------------ 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 ------------------------------oOo------------------------------
- Add a new recipe to your main program:
.
recipe2 = [ "Bechamel Sauce", "1/4 cup butter", "2 tbsp flour", "1/4 cup milk"]
.
- Question 7
- Make your program print both recipes with separator lines. The output of your program should look something like this:
------------------------------oOo------------------------------ Smoked Salmon Tortellini with Bechamel Sauce ------------------------------oOo------------------------------ 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 ------------------------------oOo------------------------------ ------------------------------oOo------------------------------ Bechamel Sauce ------------------------------oOo------------------------------ 1/4 cup butter 2 tbsp flour 1/4 cup milk ------------------------------oOo------------------------------