Modules, More Math, and Debugging - Chapter 3
Functions (Covered more in Chpater 6)
- function - like in algebra, a function can take input (arguments) and produce output (return
value). It performs a calculation or executes other code. We'll write our own functions later, but for
now we are using pre-defined functions, such as print and input.
- function call - a line of code that executes a function.
- arguments - the data passed into a function when it is called. Also called actual
parameters
- return value - the "answer" produced by the function, returned from it to the function call.
Modules and Python versions
- module - a file you can import into your program, with pre-defined Python code you can access.
- For example, the "sys" module has system-specific parameters and functions. You can use it to see what version of Python you're using.
In this class we are using Python 3. If you use Python 2, you'll notice some differences, such as the way some print statements are displayed.
The following program has different output with different versions of Python:
import sys
print (sys.version)
print("The capacity of this class is", 32)
The output could be:
2.7.15 (default, Jul 31 2018, 23:03:52)
[GCC 6.3.0 20170516]
('The capacity of this class is', 32)
Where the last statement outputs at tuple (a pair of values, we'll cover this later).
Or you might get this output:
3.6.1 (default, Jun 21 2017, 18:48:35)
[GCC 4.9.2]
The capacity of this class is 32
Where the last statement outputs a combined string.
- Note: Repl.it's Python Turtle environment uses Python 2.7, even though it doesn't allow you to use the sys module.
- The Python Documentation includes the "Global Module Index" which documents all standard Python modules.
Functions in the Math Module
- You can "import math" to have access to mathematical constants and functions such as in the following code:
import math
print("pi is", math.pi)
print("square root of 2 is", math.sqrt(2.0))
print("rounding pi up to the nearest integer:", math.ceil(math.pi))
Tips for Programming with Greater Satisfaction and Less Frustration (Less Debugging)
- One step at a time: Write a few lines of code, test them to make sure they work, then add a few more. Don't try to write the whole program before testing any of it. That way, errors will be easier to handle, and you'll make fewer mistakes.
- Use print statements to see what's in your variables, and make sure things are working as you expect. (You can always remove them later.)
- When you get errors, take the time to figure out what they mean, working backwards from the error (maybe a previous line is actually the source of the error.)
- Test thoroughly to make sure you get corrects results, especially at the boundary conditions.
Common Error Messages
- ParseError - syntax errors that are usually due to missing or incorrect punctuation
- TypeError - data type errors that are usually due to incorrect types, such as trying to add a float to a string.
- NameError - almost always means that you have used a variable before it has a value. Maybe you misspelled the variable name?
- ValueError - A function's parameter is not the right kind of value. For example, maybe you're converting a string to a float, but the string is not numeric. Might depend on what the user enters.
In-class exercise: You're planning a wedding, and you need to transport all your wedding guests from the
ceremony to the reception. A bus can carry 40 passengers. Please write a program to input the names of the bride(s) and groom(s),
and how many guests are coming to the wedding. Then ouput the
names of the couple, followed by how many busses are needed, and
the number of extra people you could carry with those busses (empty seats). You might want to use the "ceil" function which is defined in the math module.
Sample Output:
'''
Please enter the name of the bride or groom: Noah
Please enter the name of the other bride or groom: Andy
How many guests are coming to your wedding? 70
For the wedding of Noah and Andy you will need 2 busses.
You will have space for 10 extra guests on those busses.
'''
'''
Please enter the name of the bride or groom: Ellen
Please enter the name of the other bride or groom: Mark
How many guests are coming to your wedding? 120
For the wedding of Ellen and Mark you will need 3 busses.
You will have space for 0 extra guests on those busses.
'''
busses.py: Solution to above in-class exercise