[Tutor] program spontaneously closes...

Luke Paireepinart rabidpoobear at gmail.com
Sun Nov 26 09:25:06 CET 2006


Bob has answered your original question, but I'd like to make some 
observations.
Alan Gilfoy wrote:
> Hello All.
> I've been concocting some simple programs based on an online tutorial,  
> and I, of course, save a program as a .py file.
>
> When running my program via IDLE, once I've cleaned out the bugs, it  
> works as I expect it to.
>   
Perhaps the way you expect it to work is not the way it should work :)
> But I noticed something else:
>
> Clicking directly on the file's icon has the program open in the  
> Windows command line program as a .exe file.
>   
I'm not really sure why you say 'as a .exe file.'  Nowhere along the 
line is your python script compiled into an exe file.
Yes, Python itself is an executable, but your script is not, unless you 
explicitly make it one.
> The command line program seems to work through my program OK, but the  
> command line program shuts down immediately after the program has run  
> through, as opposed to staying open in order to display the result. [I  
> noticed that if the command line program is not waiting for a resposne  
> to a prompt, it stays open.]
>   
I think you mean 'if the command line program IS waiting for a response 
to a prompt, it stays open.'
And what's all of this 'command-line' business?  Your python script is 
the same when run by double-clicking as it is when run from idle:
it's interpreted by Python.  IDLE happens to have a wrapper for the 
interpreter that captures all the output, so that it can display the 
tracebacks in nice pretty red colors,
and things like that.  But it is still the same as when you run it by 
double-clicking the script.
> Relevant code block:
>
> #rectangle stuff
> length = int(raw_input("Please enter the length of your rectangle, as  
> a whole number : ")) #lets user input a number
> width = int(raw_input("Please enter the width of your rectangle, as a  
> whole number : ")) #lets user input a number
> area = length * width
> print " "
> print " "
> print " "
>   
If you're trying to make blank lines, you don't need to print a blank space.
This could just as well be
print ""
or even
print
but a better way, in my opinion, to do this is to add newlines!

eg.
print "\n"*3
or
print "\n\n\n"

> print "the length is"
> print length
> print " "
> print "the width is"
> print width
> print " "
> print "So the area is"
> print area
> print " "
>   
and all of this could be shortened to:
print "The length is " + length + ",\nand the width is" + width + 
".\nThe Area, therefore, is " + area + ".\n"
or even
print "Length: %s, Width: %s, Area: %s." % (length, width, area)
But of course you don't have to listen to my suggestions,
particularly if their functionality doesn't make sense.
No reason to copy-pasta.
It's more important that you know what the crap is going on with your 
code :)


On a final note, consider if the Python interpreter didn't automatically 
exit whenever it was done executing code:
So we've been doing console IO for a few days, and we're moving on to a 
GUI app.  So we make an app that
edits MP3 version 1 tags, say.  Now, whenever someone messes with the 
tag and exits our program, there's this crazy dos box that just sits 
there saying
'press any key to exit!' or something, and otherwise it's completely blank.
Is that what you want to happen?
Just remember that Python is used for many things besides console IO, 
and it makes everything much simpler if Python just requires you to put 
a sleep(10)
at the end of your program, or a raw_input, if you want to view the results.

Eventually you'll get into scripts that have a menu, that's displayed in 
a loop, and then you'll have a 'quit' option to exit out of the loop.
Otherwise, the loop just runs through again and you can look back at 
your previous data if you want.
SO in this case you could see the previously calculated rectangle values 
while it's asking you for the new data.

HTH,
-_-Luke


More information about the Tutor mailing list