官术网_书友最值得收藏!

Ensuring that the Python modules are present

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.

How to do it...

  1. In a text editor type the lines given in the following code.
  2. Save this as a file named simple_2.py, inside the directory called constr as we did previously.
  3. As before, open up an X terminal or a DOS window, if you are using MS Windows.
  4. Change directory into constr - where simple_1.py is located.
  5. Type python simple_2.py and our program should execute. The result should look like the following screenshot:
    How to do it...

    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"

How it works...

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.

There's more...

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.

主站蜘蛛池模板: 镇宁| 乌苏市| 武宁县| 忻州市| 渭源县| 新泰市| 通许县| 西宁市| 慈溪市| 堆龙德庆县| 宜宾市| 高淳县| 绥德县| 临泽县| 深圳市| 崇明县| 高要市| 桐乡市| 泽州县| 新沂市| 巧家县| 越西县| 景宁| 通河县| 枣庄市| 广南县| 塔城市| 芦溪县| 博野县| 临漳县| 双鸭山市| 浏阳市| 新干县| 当雄县| 罗江县| 万载县| 广南县| 当阳市| 麻江县| 开鲁县| 东乡县|