Programming Tutorial Four | |
---|---|
This example will use what we use in the previous 3 examples, but we will add 2 important
features:
Instead of having a fixed number of cars to process, we will let the user determine how many cars to process. Since we could have a big list of cars to output, we will clean up the output by using a tabular output arrangement. (**NOTE** This is similar to what you will do for lab 14 but is not exactly the same. This is a practice program to give you a feel for what the lab asks you to do -- MAKE SURE TO FOLLOW YOUR LAB INSTRUCTIONS TO WRITE THE CORRECT PROGRAM !!) |
|
Program Description -- | |
Input -- Same as the 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:
|
|
Creating the Program -- | |
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. The following link will take you to the Python Command Reference |
|
Your file should look something like this: |
Add the new variables needed for the new functionality of program four. |
Notice that we have taken a new approach in storing variables here. We are using 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") |
Be sure to notice that these are indented. It will not work without the indent. |
It is not time for the program to do some calculations. As it turns out, we have 2 kinds of
calculations in this program:
|
Notice the role that carIndex variable now plays in this program. |
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. |
|
Save your program to your student disk. Change the name of the program so you know this is
the completed version.
Note: Be sure the name ends with ".py -- this is what lets the Python Interpreter identify and run your program. |
Testing Your Program -- | |
Here is an example of what you should see: | |
Now you are ready to try the fourth program assignment.
It functions a lot like this example so use this example as a tool to help you complete assignment 4 |
|
Return to Assignments Index |