Why do I get this error?
Chris R. Timmons
ctimmons at NOSPAMlgrs.com
Sat May 19 21:10:09 EDT 2001
the_2nd_coming <jpetzold at twmi.rr.com> wrote in
<CzkN6.6821$Zb.64964 at typhoon.mw.mediaone.net>:
>
>I thought that by doing that I would assign the second charactor
>in list to n2, what do I need to do to acomplish this?
Jeremy,
"list" is a list, not a string. Looking for "the second charactor in
list" doesn't make any sense.
I get the sense you are either new to Python, or new to programming
altogether. No problem. I'm relatively new to Python, but have been
programming in other languages (Delphi, Clarion, Java, etc.) for over
twenty years.
A good technique to use when writing small programs like this is to
map out the steps the program has to execute using "pseudo code",
usually referred to as p-code. These are just simple english
sentences written in a semi-structured manner. For your program, the
p-code might look like this:
# Get input from user.
Input should be in the general form
<number><operator><number>. Allowable operators are
+ - * X x /
# Attempt to parse input into three parts:
# First number, operator, second number.
Search the input for a single occurance of any of the
allowable operator characters.
Was one found?
Yes - save it in a variable (operator), and its position
in the input string (operator_pos).
No - display an error message and exit the program.
Using the position of the operator character, extract all
text to the left of the operator character into a variable
(number1).
Try converting number1 to a numeric type to make sure it
is really a number. If an error occurs, display an error
message and exit the program.
Repeat the above code to extract and validate the number to
the right of the operator (number2).
# Perform the operation indicated by operator.
if operator is a plus sign then
result = number1 + number2
else if operator is a minus sign then
result = number1 - number2
etc...
# Display results to user.
print '%d %s %d = %d' % (number1, operator, number2, result)
HTH,
Chris.
---------------
More information about the Python-list
mailing list