![](https://csdnimg.cn/release/download_crawler_static/10173859/bg12.jpg)
1.9. THE IDLE EDITOR AND EXECUTION 18
If the Shell window does not automatically come to the foreground, select it. You should see a line
saying “RESTART” and then the start of the execution of the Mad Lib program with the cursor waiting for
your entry after the first prompt. Finish executing the program. Be sure to type the final requested Enter,
so you get back to the interpreter prompt: >>>
Look at the editor window again. You should see that different parts of the code have different colors.
String literals are likely green. The reserved words def are likely orange. Look at the last two lines, where
the identifier tellStory is black, and the identifier input is likely purple. Only identifiers that are not
predefined by Python are black. If you create an identifier name, make sure Idle shows it in black.
1.9.2. A Bug Possible When Restarting Program Execution in Idle. When you execute a
program from the Idle Editor, the interpreter gives a banner saying “RESTART”, meaning that all the
things you defined in any shell session so far are wiped clean and the program you are running starts fresh.
There is one egregious exception to that, that was still present at least in the version of Idle for Python 3.1
in Windows. We will try to demonstrate the bug. (A bug is an error in a program.)
Start running the Mad Lib program again by going to the Editor Window containing madlib.py, and
start running the program again, but do not continue....
You should see a prompt for user input generated by the program. Ignore this prompt and go back to
the Edit Window and start the Mad Lib program again.
If this bug is still present, you should see a difference in this restart: This time after the RESTART
banner and the interpreter prompt: >>>, which looks innocent enough, but this program should show the
program’s prompt string for input.
The problem only comes up because you interrupted the last execution when user input was being waited
for. The restart was not complete here: The system is still looking for the pending user input from the last
execution.
The fix is simple: Make sure the Interpreter Window is the currently selected window, and press return
to terminate the lost user input. In some circumstances, you may need to press return a second time.
After that the program should start up normally with its prompt.
Watch out for this behavior, and remember the fix.
1.9.3. The Classic First Program . Make sure you have Idle started in your Python directory (in
Windows with the provided Idle shortcut link), where you will store program files. (Do not start Idle from
the Windows Start Menu!) If you just started Idle now, you may already have a blank Edit Window in
front of you. If not, open a new window by going to the File menu and selecting New Window. This gives
you a rather conventional text editing window with the mouse available, ability to cut and paste, plus a few
special options for Python.
Type (or paste) the following into the editor window:
print(’Hello world!’)
Save the file with the File menu -> Save, and then enter the file name hello.py. Python program files
should always be given a name ending in ".py", and you must enter the .py extension explicitly .
If you look in the editor, you should see that your text is color coded. The editor will color different
parts of Python syntax in special colors. (In version 2.4 of Python, the coloring only happens after you save
your file with the ’.py’ ending.)
Now that you have a complete, saved program, choose Run menu -> Run Module. You should see the
program run in the Python Shell window.
You just wrote and executed a program. Unlike when you use the shell, this code is saved to a file in
your Python folder. You can open and execute the file any time you want. (In Idle, use File->Open.)
To the interpreter, a program source file corresponds to a Python module. We will tend to use the more
general term: a program file is a module. Note the term from the menu when running the program.
Distinguish program code from Shell text: It is easy to confuse the Shell and the Edit windows. Make
sure you keep them straight. The hello.py program is just the line
print(’Hello world!’)
that you typed into the edit window and saved. When you ran the program in Idle, you saw results in the
Shell. First came the Restart notice, the one-line output from the program saying hello, and a further Shell
prompt: