tags:

views:

100

answers:

3

Hello. Im brand new with python and im just trying some basic programming. I was wondering if there was any way i could stop python.exe from closing immediatly after it completes. It closes faster than I can read the output. Is there any way to stop that? Here is the program.
Width = float(input("Enter the width: "))
Height = float(input("Enter the height: "))
area = Width * Height
print("The area is", area, "square units.")

also would anyone be able to tell me how to get the program to repeat, say i needed it do do more than one question. and is there any way i can do it like "would you like to do another one? if yes then restart if no then close." something like that? Thank you.

+3  A: 

It looks like you are running something in Windows by double clicking on it. This will execute the program in a new window and close the window when it terminates. No wonder you cannot read the output.

A better way to do this would be to switch to the command prompt. Navigate (cd) to the directory where the program is located and then call it using python. Something like this:

C:\> cd C:\my_programs\
C:\my_programs\> python area.py

Replace my_programs with the actual location of your program and area.py with the name of your python file.

Manoj Govindan
ok. that works but is there no way to just run area.py without it closing after it spits out "The area is X square units"? for example, the computers at my college dobt allow access to cmd.exe so how would i go about doing this. And ty for the timely responce.
Clinton
dont* response*
Clinton
"the computers at my college dobt allow access to cmd.exe" - Laughable at best. They allow you to run a python script but not use cmd.exe. Pitiful security.
mathepic
@Clinton, you can use the `-i` flag to drop to a shell after a script is done. `python -i script.py`. This is very handy for debugging because you are in the namespace of the module defined by the script.
aaronasterling
@mathepic... i totally agree.@aaronasterling.... ty.
Clinton
+1  A: 

You can't - globally, i.e. for every python program. And this is a good thing - Python is great for scripting (automating stuff), and scripts should be able to run without any user interaction at all.

However, you can always ask for input at the end of your program, effectively keeping the program alive until you press return. Use input("prompt: ") in Python 3 (or raw_input("promt: ") in Python 2). Or get used to running your programs from the command line (i.e. python mine.py), the program will exit but its output remains visible.

delnan
perfect. just what i was looking for. tyvm.
Clinton
A: 

Auxiliary answer

Manoj Govindan's answer is correct but I saw that comment:

Run it from the terminal.

And got to thinking about why this is so not obvious to windows users and realized it's because CMD.EXE is such a poor excuse for a shell that it should start with:

Windows command interpreter copyright 1999 Microsoft
Mein Gott!! Whatever you do, don't use this!!
C:>

Which leads me to point at http://stackoverflow.com/questions/913912/bash-shell-for-windows

msw
I have little exposure to non-windows OSs and their shells, but I realize bash is vastly superior to cmd. However, it's not as bad as some people claim - you can get basic stuff (navigating through the file system, invoking scripts and pre-built administration tools, etc) done. It's batch that really sucks. Plus, it has little to do with the question. Therefore, with a whispered "sorry", -1
delnan
+1. vaguely related and you make _great_ , if slightly unoriginal, points. don't let a sour windows user get you down: they're just jealous.
aaronasterling
I've used JCL, CP/M, TOPS-10, and even VMS. The DOS CMD shell is really that bad. And although the point may be unoriginal, it was a bit of an epiphany to me that having to work with that interface likely actively discourages its use for the population generally (as it did for me when I've been forced by circumstance to use it).
msw