Programming Tutorial Two | |
---|---|
In this example, you will create a sequential control structure. The program will ask
for input, perform some calculations and then deliver some output. Since this program has
no loops, the program is only executed once.
(**NOTE** This is similar to what you will do for lab 12 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 -- | |
Download:
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: |
Variables are used to reserve room in computer memory so we have a place to put our data. Some programming languages require you to declare your variables in advance. Python lets you declare your variables as you need them, however it is a good practice to think about what variables you will need and provide a space in memory for them. |
|
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. |
|
Now we need to ask the user for the input data. When data comes into the program, we need
to make sure that the computer understands what type of data it is. In Python, the computer
will assume the data is a character string unless we specify or cast the data as something
else.
If we want the data to be an integer, we will wrap the input statement in an int( ) cast -- telling the computer to interpret what the user enters as an integar. If we want the number to show fractions of a whole number (the term is floating point) we will use a float( ) cast. |
|
It is now time for the program to do some calculations. As it turns out, we have 2 kinds of
calculations in this program:
|
|
Notice that the variable "totalMiles" is derived from 2 variables previously entered by the user -- endMiles and startMiles -- so I can place this code anywhere after the "User Input" section of the program |
|
Notice that one of the variables here (totalMiles) was not entered by the user. It was calculated in the previous step or the totalMiles variable will be incorrect |
|
Same comment from the last section -- the variable milesPerGallon was calculated in the last step, so this step must come after it | |
The last task our program needs to perform is output. This is what allows you (the human) to see what the computer did and reap the rewards from the computers fast calculation ability. | |
Notice that when you print something back to the screen, you have to specify if it is just a label for a variable (a text string) or the variable itself. The example below shows the text string labels wrapped in double quotes ("Text String") and the variables listed without any quotes. |
|
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 -- | |
If it is not already on your computer, you can go get the Python Interpreter from www.python.org | |
See the first programming example for details on how to set it up | |
If the Python Interpreter is already on your computer, you should be able to double-click
on the file you created and the program will run in a window.
Here is an example of what you should see: |
|
Now you are ready to try the second program assignment. It functions a lot like this example so use this example as a tool to help you complete assignment 2 | |
Return to Assignments Index |