Understanding Function Definitions: A Case Study of the 'dscore' Function, Slides of Introduction to Computing

How to define a function named 'dscore' in python, which takes a string as an argument and returns the difficulty score represented in the string. The anatomy of a function definition, the role of parameters, and the importance of specifications and preconditions. It also discusses testing and debugging techniques.

Typology: Slides

2012/2013

Uploaded on 08/17/2013

bakul
bakul 🇮🇳

4.6

(16)

69 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Users Want Functions!
Given: info contains a comma-separated string with last name, difficulty,
execution, and penalty.!
§Example: info = 'RAISMAN, 6.7, 9.1,0'!
Goal: store the difficulty as a string, with no extra spaces or punctuation, in
variable df!
Users (including other programmers) want to write things like:!
raisman_df = gym.dscore('RAISMAN, 6.7, 9.1,0')!
print gym.dscore(' PONOR , 6.2 , 9.0 , 0')!
The function dscore is in module (file) gym. !
When called, it returns a value that the user can utilize as they wish.!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Function Definitions: A Case Study of the 'dscore' Function and more Slides Introduction to Computing in PDF only on Docsity!

Users Want Functions

Given : info contains a comma-separated string with last name, difficulty, execution, and penalty. § Example: info = 'RAISMAN, 6.7, 9.1,0' Goal : store the difficulty as a string, with no extra spaces or punctuation, in variable df Users (including other programmers) want to write things like: raisman_df = gym.dscore('RAISMAN, 6.7, 9.1,0') print gym.dscore(' PONOR , 6.2 , 9.0 , 0') The function dscore is in module (file) gym. When called, it returns a value that the user can utilize as they wish.

"""Returns: difficulty score, as a float, represented in info. Precondition: info is a string with commas separating its component values: last name, difficulty score, execution score, penalty.""" startcomma = info.index(',') tail = info[startcomma+1:] # part of info after 1st , endcomma = tail.index(',') return float(tail[:endcomma].strip()) def dscore(info) :

Anatomy of a Function Definition (I)

header In file gym, we define dscore as follows. declaration of parameter (variable) named "info" body ( indented ) specification, as a docstring Return statement (optional). Contains expression whose value results from the function call.

Anatomy of a Specification: 

User Documentation

def dscore(info): """Returns: difficulty score, as string, represented in info. Precondition: info is a string with commas separating its component values: last name, difficulty score, execution score, penalty.""" startcomma = info.index(',') tail = info[startcomma+1:] # part of info after 1st , […] Single summary line, followed by blank line. (More detail can be added in separate paragraphs) Precondition: assumptions about the argument values

A Specification is a Contract

Preconditions are a promise that:

§ if the arguments satisfy the preconditions, the function works as described in the specification; § but, if the user's arguments violate the precondition, all bets are off.

So write these contracts carefully!

Common sources of software errors:

§ Preconditions not documented properly § Functions used in ways that violate preconditions

>>> gym.dscore('Raisman; 6.7, 9, 0')

"I'm sorry Dave, I'm afraid I can't do that"

Organizing Test Cases: Unit Tests

• A unit test is a module that tests another module

§ It imports the other module (so it can access it)

§ It imports the cunittest module (provided by us)

§ It defines one or more test procedures

  • Evaluate the function(s) on the test cases
  • Compare the result to the expected value

§ It has special code that calls the test procedures

• Our test procedures use the cunittest functions. Ex:

def assert_equals(expected,received):

"""Quit program if expected and received differ"""

Example unit test: last_name_first(n)

test procedure

def test_last_name_first(): """Test procedure for last_name_first(n)""" cunittest.assert_equals('White, Walker', last_name_first('Walker White')) cunittest.assert_equals('White, Walker', last_name_first('Walker White'))

Application code

if name == 'main': test_last_name_first() print 'Module name is working correctly' Expected is the literal value. Message will print out only if no errors. Quits Python if not equal Received is the expression.

Debugging with Print Statements

Print statements expose the values of variables, so

you can check if they have the value you expect.

print 'in this solution, df is :' + df + ':'

Don't leave these in your finished code! They

reduce readability.