more troubles with my program
the_2nd_coming
jpetzold at twmi.rr.com
Mon May 21 22:26:02 EDT 2001
thank-you for all the help you have given this newbie, not only to python
but also programming.
I am still having troubles with my program, it tells me that I am trying to
apply a mathematical operator to non integers.
hear is my source:
------------------------------------------------------------------
def evaluate(list):
num1 = list[0]
num2 = list[2]
operator = list[1]
print num1
print num2
if operator == '+':
answer = num1 + num2
elif operator == '-':
answer = num1 - num2
elif operator == 'X':
answer = num1 * num2
elif operator == 'x':
answer = num1 * num2
elif operator == '*':
answer = num1 * num2
elif operator == '/':
answer = num1 / num2
else:
print 'invalid operator'
def fix_expression(list):
word = list
print word
l = []
n1 = word[0]
n2 = word[1]
n3 = word[2]
l.append(n1)
l.append(n2)
l.append(n3)
evaluate(l)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
import string
expression = raw_input('please enter your singel operator expression')
string.strip(expression) # get rid of all leading and trailing white space#
list = string.split(expression)
if len(list) >1:
print evaluate(list)
elif len(list) == 1:
print fix_expression(list[0])
else:
import sys
sys.exit
-------------------------------------------------------------------------------
could it be due to the fact that I use raw_input?
I would think that once you take a number out of a character string you
should be able to perform math functions on it. am I wrong?
thanks
J.
More information about the Python-list
mailing list