CSC111 Lab 7 2011

From dftwiki3
Revision as of 09:46, 19 October 2011 by Thiebaut (talk | contribs) (Testing with the if statement)
Jump to: navigation, search

--D. Thiebaut 10:37, 19 October 2011 (EDT)


Testing with the if statement

  • 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!



Functions and Stick Figures

StickFigureAnimated2.gif

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.


Question 1
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().


Question 2
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.
  • 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.


Question 3
Modify your program and add the definition of a new function called roundLongFigure() which will print a round-bodied long-legged stick figure. Your new function should not contain print statements, but only calls to other functions that are already part of your program. Call it from your main program after you call skinnyStickFigure(), as follows:


def main():
    skinnyStickFigure()
    roundLongFigure()





Happy Birthday with Functions!

HappyBirthdayAnimated.gif
  • Write a program that will use functions and that will display a "Happy Birthday" message on the screen.
  • Requirements. Your program should contain:
    • One function, hbday(), prints "Happy birthday to you"
    • One function, dear(), gets the name of the person and prints "Happy birthday, dear xxxxx"
    • One function, singSong(), gets the name of the person and prints the whole song with the name of the person.
    • The main program asks the user for a name and "sings" the song:


              def main():
                  name = raw_input( "person's name? " )
                  singSong( name )


Question 4
Go ahead and write the program.
Question 5
Challenge of the day
Assume that you need to print the song for everybody in the class. We saw earlier how to write a loop that prints all the accounts of the form 111c-aa, 111c-ab, 111c-ac, ... 111c-az. Modify your program so that it prints a happy birthday song for every 111c account!





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------------------------------