Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/Week 2 - Lab 1: Code Organization: Input/Output, Code Documentation, and Testing

Skills

 * Input/output (data that goes into an algorithm and the data that come out)
 * Code documentation
 * Testing

Input/Output Functions
Using JES, you should see in the bottom panel of the main window a console. The >>> is a Python prompt indicating that the interpreter is waiting for us to give it a command. In programming languages, a complete command is often called a statement.

A function is how you create a sequence of statements together. Here is an example of creating a new function called hello:

>>>def hello: print "hello" print "computers are fun" >>>

the first line states that you are defining a function called hello. The following lines are indented to show that they are part of the hello function. The blank line following the last statement (hitting the key twice) lets the program know that you are finishing defining a function and the interpreter responds with another prompt >>> if there were no errors in creating the function.

A function is invoked by typing its name. Here is an example of what happens when we use the hello command:

>>> hello hello computers are fun >>>

The two print statements from the hello function are executed in sequence.

The next example will show you how you can pass Strings, numbers and other types of information to a function for use. It puts a person (which is a string) within the two parentheses, this is know as a parameter. >>>def greet(person): print "Hello", person print "How are you?" >>>

Now to use this function you will do the following >>> greet ("John") Hello John How are you? >>> greet ("Emily") Hello Emily How are you? >>>

With the greet function we can send different names to customize the output.

This example will show you how to get input from the keyboard. x = input ("Enter a number between 0 and 10: ")

Here x is a variable, which will be used to store a number from 0 and 10. The entire line is an input statement, the program with display the message "Enter a number between 0 and 10:" and then waits for the user to type some information on the keyboard, followed by pressing the  key. The value that the user types in is now stored as the variable x.

Now that we are familiar with inputs and outputs, how do we use this with functions? You will create a function that will take an input (temperature in degrees Celsius) and will convert to degrees Fahrenheit.

celsius = input("what is the celsius temperature?") fahrenheit = convertCelsiusToFahrenheit(celsius) print "The temperature is ", fahrenheit, "degrees Fahrenheit."

Now we define the convertCelsiusToFahrenheit function as follows:

def convertCelsiusToFahrenheit(celsiusTemperature) convertedTemperature = (9.0 / 5.0) * celsius + 32 return convertedTemperature

Code Documentation
You can document a Python function by giving it a doc string.

Example 2.2. Defining the buildConnectionString Function's doc string def buildConnectionString(params): """Build a connection string from a dictionary of parameters.   Returns string.""" Triple quotes signify a multi-line string. Everything between the start and end quotes is part of a single string, including carriage returns and other quote characters. You can use them anywhere, but you'll see them most often used when defining a doc string.