- Python 2.6 Graphics Cookbook
- Mike Ohlson de Fine
- 648字
- 2021-04-09 22:39:33
Here is a slightly longer version of the previous program. However, the following modules are commanded to "report for duty" inside our program even though they are not actually used at this time: Tkinter
, math
, random
, time
, tkFont
.
We need the assurance that all the Python modules we will be using later are present and accessible to Python, and therefore, to our code. Each module is a self-contained library of code functions and objects that are called frequently by the commands in your programs.
- In a text editor type the lines given in the following code.
- Save this as a file named
simple_2.py
, inside the directory calledconstr
as we did previously. - As before, open up an X terminal or a DOS window, if you are using MS Windows.
- Change directory into
constr
- wheresimple_1.py
is located. - Type
python simple_2.py
and our program should execute. The result should look like the following screenshot:This proves that your Python interpreter can access the necessary library functions it will need.
""" Program name: simplest_2.py Objective: Send more than one line of text to the screen. Keywords: text, simple ====================================== Author: Mike Ohlson de Fine """ import Tkinter import math import random import time import tkFont print "=======================================" print "A simple, apparently useless program like this does useful things:" print "Most importantly it checks whether your Python interpreter and " print "the accompanying operating system environment works" print " - including hardware and software" print "======================================" print " No matter how simple it is always a thrill" print " to get your first program to run"
The print
command is an instruction to write or print any text between quotation marks like "show these words" onto the monitor screen attached to your computer. It will also print the values of any named variables or expressions typed after print.
For example: print "dog's name:
", dog_name
. Where dog_name
is the name of a variable used to store some data.
The print
command is very useful when you are debugging a complicated sequence of code because even if the execution fails to complete because of errors, any print commands encountered before the error is reached will be respected. So by thoughtful placing of various print statements in your code, you are able to zero in on what is causing your program to crash.
When you are writing a piece of Python code for the first time, you are often a bit unsure if your understanding of the logic is completely correct. So we would like to watch the progress of instruction execution in an exploratory way. It is a great help to be able to see that at least part of the code works. A major strength of Python is the way it takes our instructions one at a time and executes them progressively. It will only stop when the end is reached or a when programming flaw halts progress. If we have a twenty line program and only the first five lines are bug-free and the rest are unexecutable garbage, the Python interpreter will at least execute the first five. This is where the print
command is a really potent little tool.
This is how you use print and the Python interpreter. When we are having trouble with our code and it just won't work and we are battling to figure out why, we can just insert print statements at various chosen points in our program. This way you can get some intermediate values of variables as your own private status reports. When we want to switch off our print watchdogs we simply type a hash (#
) symbol in front, thus transforming them into passive comments. Later on, if you change your mind and want the prints to be active again you just remove the leading hash symbols.