need help with python

James T. Dennis jadestar at idiom.com
Sun Jun 10 15:54:59 EDT 2007


adamurbas at hotmail.com wrote:
> On May 11, 10:16 pm, Paul McGuire <p... at austin.rr.com> wrote:
>> On May 11, 9:41 pm, adamur... at hotmail.com wrote:

 [... much ellided ...]

	["ellided" is a fancy word for "left out" or "replaced
	 with ellipses."]

> I was looking around in my Python folder and saw something to do with
> that IDLE thing you were talking about.  When I right clicked on a .py
> file, it said edit with IDLE.  I opened it and it was my code but each
> line was a different color.  It looked confusing so I decide to save
> it for later.  I knew that I could get the run thing to do the command
> thing, but I had forgotten how to get the black window to come up.

 Idle is an "IDE" (integrated development environment).  It's sort
 of like an editor and a "terminal window" ("command shell") combined.
 The different colors are the result of a feature called "syntax 
 highlighting" which is available in most of the modern IDEs and better
 text editors.

 To "get the black window to come up" use the [Start] menu, choose
 the "Run" option and type in the name of the program: "cmd" (then
 hit [Enter]).

 You can can also find it if you chase far enough down the various
 sub-menus off the [Start] menu.


> Ok.  Well, I tried to us the cmd window.  It says python: can't open
> file 'area.py'  I'm guessing that's not good.  It won't open any of
> my .py files.  It's because of where I saved them.  I can see how this
> i going to work now.  Ok so I'll just move them to the place that the
> command line says.  Now it still won't run my other program:

 Yes it's because of where you saved them and because of where the
 command prompt (The C:\> thing that you see inside the "terminal window").

 Rather than moving your .py files to wherever your prompt is pointing,
 it's generally better to change your prompt to point to the right place.

 For example if you've been saving you .py files to "C:\My Documents" then
 type: 

	cd "\My Documents" 

 ... (with the quotes around it). 

 Then try to run your files from there.

> # Area calculation program

> print "Welcome to the Area calculation program"
> print "-------------"
> print

> # Print out the menu:
> print "Please select a shape:"
> print "1  Rectangle"
> print "2  Circle"

> # Get the user's choice:
> shape = input("> ")

> # Calculate the area:
> if shape == 1:
>    height = input("Please enter the height: ")
>    width = input("Please enter the width: ")
>    area = height*width
>    print "The area is", area
> else:
>    radius = input("Please enter the radius: ")
>    area = 3.14*(radius**2)
>    print "The area is", area

> Perhaps it isn't written correctly.  I don't think it likes the pound
> signs.  I'm not sure.  But, I did go to that mailing list you
> recommended.  Thanks for that.

 The pound signs are used by Python to mark "comments" (stuff the
 Python interpreter ignores; that's there just as hints and reminders
 to humans who are reading the source code).

 So, from the first # sign on a line until the end of the line Python
 is ignoring everything.

 The only exceptions to this rule are when the # is inside quotes:

	print "This is technically called an octothorpe: #. See?" 

 ... in that line the octothorpe (#) is part of a quoted string
 so it and the text following it are NOT ignored.

 Incidentally, I would use the raw_input() function in place of
 the input() function that these examples have been using.

 The problem with the Python input() function is that it parses 
 the input as if it were Python code.  So it will raise an 
 exception for any input you enter which is NOT valid, legal 
 Python code.  (So far you've been lucky in that you've only been
 using it to enter simple numbers, which are, of course, valid
 Python expressions.

 You can actually get away with inserting one line:

	input = raw_input

 ... near the beginning of any of your code examples to alleviate
 this issue.  The raw_input() function will accept any string you can
 enter up to the first [Enter] key.  (Actually on my platform, Linux,
 it's possible to embed newline and/or carriage return characters
 --- the ASCII characters which are normally generated by the [Enter]
 key on various computing platforms --- into a raw_input() value by
 preceding each of them with a Ctrl-V key.  Just off hand I don't
 know if that works under Windows using a command prompt window.
 Just pointing that out for other readers to make the observation that
 Python's raw_input() might not be as "raw" as you might expect).


-- 
Jim Dennis,
Starshine: Signed, Sealed, Delivered




More information about the Python-list mailing list