Introduction

The lessons for M03 concentrate on making Python programs more modular using functions, so every program you write for this module should include functions to accomplish most of the processing.

Assignment Requirements and Examples Below

Programming Practices

There are a couple of things to make special note of when converting programs to use functions.

  1. Most variables should be scoped locally within functions. In other words, define variables to use in the function only in that code.
  2. Minimize the use of global variables. More on this below.
  3. Define constants by naming a global variable in all CAPS (although Python does not have real constants)
  4. Limit functions to accomplish a single result (See Limiting Functions below)

Limiting Functions

Normally, a function should be limited in what it does. This is refered to as the Single-Responsibility Principle (SRP). In other words, if the focus of a function to calculate a tax rate - that is all it should do. It should not also calculate a discount, a total, and display the results. Here are some links for reading up on the SRP:

Many times, the result of following SRP is that programs consist of many, very small functions. That is okay. For new programmers, if a function is more than 15 or so lines of code, consider whether it should be more than one function.

Global Variables

There are good reasons to use global variables. Programs should be relatively easy to maintain, so if it makes it easier to maintain a program by using global variables (within reason), then it might be appropriate. What is a good example? Take a program that must calculate a state sales tax, for example. If there is only one state, it might make sense to make the tax rate a global variable. That way, if the rate needs to be changed, it is easy to see where to do it and it applies to the entire program.

But what if there might be multiple states involved? And what if the rate depends on other things, such as what types of products or services are being purchased? In those cases, it is more appropriate to define the tax rates in functions (or ideally, in a file or database).

Literals vs. Variables

As long as we are discussing variables and scoping, let's touch base on the issues of using literals in code.

Generally, we never want to use literals, except to set the value of a variable. For example, if the tax rate is .025, it is better to define a variable and use it, rather than use a literal.

NOT ideal:

taxdue = purchaseamt * .025

Better:

taxrate = .025
taxdue = purchaseamt * taxrate

Why? Better maintainability (and better readability). As programs get more complex, it is common that there may be multiple calculations using the same values. If there are 4 different instances of using the literal values of .025, it is easier to mistype one as .25 and easier to miss one if the literal value needs to be changed.

Literals and User Prompts

It is also a good idea, in general, to create variables for any text used for user prompts. This can be especially true if the program has multiple input statements using prompts. It is easier to maintain the text for prompts if they are all in one place - at the top of function, for instance.

Assignments

Requirements

  • Every program should have a main() function that calls other functions to do work.
  • Other than printing a banner line, main() should NOT contain input, print or any other calculation statements.
  • Do all work and processing in functions
  • Every function should have at least one comment line explaining the purpose of the function

M03 Exercise #2 (Calculate Taxes)

  • You should have at least 4 functions in addition to main().
  • Do NOT return multiple values from any functions
  • You may define the tax rates as global variables, if you wish.
  • Define the text for the user prompt as a variable, but do not make it a global variable
  • You may combine the calculating of totals and displaying output in a single function
  • You should display the calculated amounts for the purchase amount (entered by the user), sales tax, state tax, total tax and total amount due.

Output example:

M03 Exercise #2 Output

Figure 1: M03 Exercise #2 Sample Output

M03 Exercise #2 Sample Code

Figure 2: M03 Exercise #2 Sample Code

M03 Exercise #12 (Maximum Value)

  • Prompt for multiple values at one time
  • 3 points extra credit for having your prompt function return multiple values
M03 Exercise #12 Sample Output

Figure 3: M03 Exercise #12 Sample Output

M03 Exercise #18 (Prime Number List)

  • No prompt needed
  • Include at least 2 functions in addition to main()
  • One function should control what numbers to loop through to check for a prime number; the other function will be is_prime()
  • Use the end='' attribute for the print() statement, if desired, to keep output on a single line (example 2)
M03 Exercise #18 Sample Output1

Figure 4: M03 Exercise #18 Sample Output 1

M03 Exercise #18 Sample Output2

Figure 5: M03 Exercise #18 Sample Output 2