[Tutor] noob question (Windows, Python 3.1)

Dave Angel davea at ieee.org
Thu Aug 6 16:03:57 CEST 2009


Michael Connors wrote:
> 2009/8/6 Joshua Harper <joshharper27 at gmail.com>
>
>   
>> Ok, so I am trying to learn python, and I am reading many tutorial type
>> things and I am kind of stuck with implementing some of the code... so for
>> example the tutorial says "*To get the examples working properly, write
>> the programs in a text file and then run that with the interpreter*"
>> Alright...simple enough so I wirte the example program:
>>
>> x = input("Please enter a number: ")
>> print "The square of that number is
>>
>> I save this as a .py and Open With>python.exe. OK, so that gives me the
>> terminal ansking me to enter a number, I enter a number and click enter and
>> then it prints in like half a nanosecond and the cmd line window closes. I
>> want to know how to have the window stay open so that, in future scripts I
>> may be able to actually see what was printed. I talked to my friend and he
>> said that he has the same problem...anybody?...help???
>>
>>
>>     
> It closes because it is finished.
>
> If you want to see the result, you could either:
>
> - Place another input("") after the print statement.
> - Run the program from the command prompt (in which case you will probably
> need to set the path)
>
>   
Michael is correct, but I'd like to expand on the answer.

In Windows, there are two kinds of applications, console apps and gui 
apps.  A console app uses stdin and stdout (character mode, like input 
and print).  A gui app has buttons, menus, and dialog boxes.  Console 
apps are much easier to write and debug, they tend to run faster, and 
tutorials always start there.

A console app can be run from an Explorer shortcut, or from the context 
menu as you're doing, but you're missing a lot.  As you've already 
noticed, the console is deleted immediately after the program 
terminates.  So you can put an extra    input("Press OK to finish")   at 
the end of your code.  But for various reasons (eg. bugs) your program 
may never reach that line.  And if it doesn't, any error messages will 
also vanish when the console does.  While there are workarounds for 
this, the real answer is to learn to use a command prompt.  At least 
until a program is bug-free.

A "command prompt"  (aka "Dos Box", or console, or shell) can be started 
from the "Run" command on the start menu --  just type   "Cmd"  as the 
program to run.  There's also a shortcut called "Command Prompt" in 
Start->Accesssories.


The default installation on Windows sets up a file association for both 
.py and .pyw files, so you should be able to just type   example.py    
at the command prompt to run the program.  The command prompt window
 will scroll to hold more input than would otherwise fit on screen.  And 
you can copy & paste from and to a command prompt, though it's tricky 
(another discussion).

You may want to create a few batch files to make your life at the 
command prompt a bit easier.  For example, the path to the python 
interpreter is not normally added to the PATH variable, and I don't 
think it should.  So if you want to run Python.exe explicitly, it helps 
to make a batch file to make that easier.  We can help with that as well.


When you run the program from the command prompt, the print output stays 
there till you either close the command prompt, or run enough other 
stuff that it scrolls off the top end of the buffer.

DaveA



More information about the Tutor mailing list