This this lab, you’ll practice writing and working with functions. Pieces of this lab are based on problems written by Dominique Thiebaut at Smith College, and it was adapted for this course in June 2018 by R. Jordan Crouser. This content was remixed and updated in 2019/2020 by Alicia M. Grubb.


Step #0: Warning

In order to be sucessful in computer science, an important skill that you must develop is understanding how to implement and develop a specification, and knowing when you can take liberty with the specification. You also need to distingush between implementing and developing a specification. In the course work for CSC111, we ask for some things very precisely. It is important that you read every word of lab and assignment descriptions. Misreading or ignoring directions will result in incorrect software/code.


Take a few minutes and introduce yourself to your lab partner. Answer the question: “If you were an ice cream flavor, which one would you be and why?”

We added prompts to remind you to switch driver/navigator roles, but feel free to switch more frequently or mid task.


Step #1: Practise - Drawing a Stick Figure

In this opening section, we’ll play around with some very simple functions to get familiar with defining and calling functions.

Open a new file in Thonny and enter the functions below:

def head():
  print( "  o" )
  
def leftArm():
  print( " /", end = "" ) 
  
def torso():
  print( "O", end = "" )
  
def rightArm():
  print( "\\" )
  
def leftLeg():
  print( " /", end = "" )
  
def rightLeg():
  print( " \\" )
  
def stickFigure():
  head()
  leftArm()
  torso()
  rightArm()
  leftLeg()
  rightLeg()
  
def main():
  stickFigure()
  
main()

Save the program as lab4p1.py, then run the program to see what it does. A few important points:

  • You may notice that something looks a little different about the print() function in a couple of places. In python3, the print() function can take an optional end = parameter. Can you figure out what it does?
  • Also pay attention to how we print the right arm and leg. Because the ‘\’ character is used to print special characters, like ‘\n’ (new line), or ‘\t’ (tab), if we want to print a regular backslash character, we have to use 2 backslashes next to each other.

At this point you should switch driver/navigator roles.


Step #2: Snowperson Function

  • Modify the code in lab4p1.py to print out a simple ASCII cartoon of your own. For example, you might try to make this snowperson:
  • Remember to use functions to organize your code in a logical way.

At this point you should switch driver/navigator roles.


Step #3: MadLibs

In the remainder of this lab, you will write a program that takes in user input and generates a humorous (somewhat random) story. You may be familiar with these popular road trip / middle school sleepover fill-in-the-blank stories as MadLibs.

1. Setting up

  • Create a new file called lab4p2.py in which you and your partner will work.
  • Add the usual header, including both your names.
  • Below the header, use comment lines to create an outline of your program: what pieces will you need? Recommended: start on the board, not the computer!
  • Save and Run the program, just to make sure you do not have any syntax errors.

2. Write/find your base story

  • MadLibs work by removing certain words from a pre-written base story and replacing them with random words that are the same part of speech (noun, verb, adjective, etc.).
  • In order to continue with the programming part of this lab, you’ll need to come up with a base story. Be creative! You could alter a well known story, use lyrics to your favorite song, or you could write your own.
  • Once you have your story, identify 6-10 words that you’re going to replace along with their part of speech. These will form the “blanks” that you will ask the user to fill in.

At this point you should switch driver/navigator roles.


3. Generate story

  • Using what you’ve learned about printing, string methods, and user input, write a program that uses functions to generate a MadLib version of your base story.
  • You will probably want functions like getNoun(), getAdjective(), getVerb(), and printStory(). For example getNoun() might prompt the user for a single noun and return it as a string. So if your story needs 4 nouns, printStory would call getNoun 4 times and insert them into the string that it prints as your story. Note: using String.format is a nice way to go if you use it like this where the order of the format arguments is plugged into the string in sequence without needing argument numbers:

    person = "Fred"
    age = 19
    message = "{} is {} years old".format(person, age)
    print(message)

4. Random Story Generator

  • You’ve probably had a couple of laughs while testing your program, but eventually any MadLib is going to start to get old.
  • Can you write a program that has a list of fill-in-the-blank sentences, and assembles them in a random order to generate a new MadLib every time?



Lab Submission


If you have time, we encourage you to get started on your homework assignment in lab this week.