This example will use what we use in the previous 3 examples, but we will add 2 important features:
|
Input -- Same as last program:
Your program will request the following information from the user:
|
|
Calculation:
Your program will perform the following calculations:
|
|
Output:
Your program will output the following data:
|
While all the steps and code you need to create this program are included in this example, you may wish to see a more complete list of commands. This link will take you to the Python Command Reference. |
| |
Add the new variables needed for the new functionality of program 3 | |
Notice that we have taken a new approach in storing variables here. We are using something called an Array (or list in Python). Not only does it allow us to store several pieces of like information together, we can use the loop index variable to help us store in a structured grid. |
|
It is common for a program to have some sort of title that tells the user what the program does and it might
include the name of the person (or Company) who wrote the program.
|
|
The user input data will be a little different since we are now using an Array (or list)
|
|
It is now time for the program to do some calculations. As it turns out we have 2 kinds of calculations in this
program: 1) a calculation of user input data and 2) a calculation of a variable that has itself been
calculated by this program. The first type of calculation I can do whenever I want to but I must do the 2nd type
of calculation after I have performed the calculation required as its input. Lets see what I mean:
|
|
Now we are really going to do something different. We are going to use Python's text formatting syntax to create a tabular output.
This next section of code creates the column labels. Each field is 18 spaces wide and the first one is left justified.
|
|
We will once again make use of the "for" loop and the carIndex variable to output all the information we stored in our
array.
|
|
One last set of information to print. The following code prints the summary and average information.
|
|
|
|
Now you are ready to try the fourth program assignment. It functions alot like this example so use this example as a tool to help you complete assignment 4. |