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.
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.
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:
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?At this point you should switch driver/navigator roles.
lab4p1.py
to print out a simple ASCII cartoon of your own. For example, you might try to make this snowperson:At this point you should switch driver/navigator roles.
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.
lab4p2.py
in which you and your partner will work.At this point you should switch driver/navigator roles.
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)
lab4p1.py
and lab4p2.py
files before the end of the day.If you have time, we encourage you to get started on your homework assignment in lab this week.