Python IDLE Tutorial
|
| Input the length in
inches Calculate centimeters as 2.54 * inches Display centimeters |
Implement the Design (Step 3)
This step is where the design is translated into a computer programming language. For our purposes we will use Python. The program will look like this when we finish coding it:
Compare the lines of Python code with the lines of pseudocode in the algorithm for similarities and differences. (Note that everything following a # on a line is a comment, and not part of the program itself.)
# in2cm.py -- January 10, 2009 -- C. J. Craig # Converts inches to centimeters def main(): print "This program converts inches to centimeters." print "(1 inch = 2.54 centimeters)" print inches = input("Enter length in inches: ") centimeters = 2.54 * inches print "That is", centimeters, "centimeters." main()
Coding. Use the Python IDLE to enter the above program code by following these steps:
- From the Start menu, open the Python IDLE program, which will open the Python shell, an interactive window in which you can type in Python instructions directly and have them immediately interpreted and executed. (Note: Instead of opening in the shell window, IDLE may open with an edit window, depending on how it was configured when it was installed. If this is the case, you can skip step 2, below.)
- From the File menu, select New Window which will open an edit window, in which you can code a complete program, edit and save it.
- From the File menu, select Save as...
- Type in2cm.py in the File name box
- Change the Save in: text box to the location where you want to save your source file. You can save it to the hard drive, a floppy diskette, zip disk, or a thumb drive.
- Click OK
- Now type the above program in the edit window, using the normal Windows editing keys (backspace, arrow keys, etc.) and mouse pointer. Use your name, not "C. J. Craig". Notice that as you enter the code, the colors indicate what type of code you are entering:
Blue: a Python definition
- Red: a comment
- Green: a string constant
- Orange: a keyword in Python
Black: everything else
- Save your work periodically by choosing Save from the File menu, or clicking on the diskette icon on the button bar
- When you have finished entering the program, save it one more time. You are now ready to invoke the interpreter, which will process your program one line at a time, converting your source code into machine language object code and executing it
- From the Run menu, choose Run module.
- The program will begin to execute in the Python Shell, by displaying the prompt "Length in inches:"
- As the program executes, the interpreter may encounter some syntax errors. Depending on the type of error, it may identify them in by highlighting a line in the edit window, or by identifying the offending line during execution in the Python shell.
- In either case, compare that line with the program above and:
- Edit the line as necessary
- Resave your source code
- Run the program again
- Repeat until you have eliminated all syntax errors. If you have a syntax error you just can't find, ask your instructor.
- When you have corrected all your syntax errors, you have a clean compile, and are ready to start testing your program for logic errors.
Using Help. To correct syntax errors, you may need to refer to the Python's built-in Help facility. A hallmark of a good programmer is familiarity with Help – the ability to find what you are looking for quickly. The Python IDLE has extensive information on the Python language.
- From the Help menu, select Python Docs
- Help will open with the content window, with links to major topics, including the Python language reference and a tutorial. This is useful if you are just browsing, but if you are trying to find information on a specific item, it's better to use the index and/or the find feature:
- After finding and reviewing the information, you can close the help facility, or leave it open for future reference and return to the IDE.
Testing the Solution (Step 4)
Testing your program will require running it, inputting test values, observing the results, and comparing these results with what you expected. If the observed results are different than the expected results, your algorithm – and therefore the program on which it was based – has a semantic (logic) error.
- Write down several test values as input, and manually determine the correct conversions. For example:
1 inch = 2.54 centimeters
- 22.5 inches = 57.15 centimeters
72 inches = 182.88
- From the edit window, run your program (Run | Run Module)
- Enter test values and observe the results, comparing them to what you expected.
- Rerun the program to test other values. Did you get what you expected?
- If you obtain unexpected results, ask your instructor to take a look at your source code, since this example should contain no logic errors.
Maintaining the Program (Step 5)
This step is really a never-ending process, as you design and implement improvements to the program in response to changes in the needs of the user. How could this program be enhanced?
| Updated: 08.25.2009 |