more troubles with my program

Malcolm Tredinnick malcolm at commsecure.com.au
Mon May 21 23:11:01 EDT 2001


On Tue, May 22, 2001 at 02:26:02AM +0000, the_2nd_coming wrote:
> 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.

The error message is correct. :-)

[...snip...]

> 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?

Yes, everything is still a string here. raw_input() returns a string.
Applying split() to a string returns a list of strings, so you pass two
strings to evaluate().

Some languages, like Perl, try to guess that if you apply the '+'
operator to strings that might be turned into integers, then it should
treat them as integers. In Python that is not done (and with good
reason).

You really need to apply the int(), long() or float() functions to your
strings before trying to treat them as numbers (which function you use
depends on what your expected input is). Each of these functions will
raise a ValueError exception if they cannot do the conversion (for
example, float('elephant') doesn't have a reasonable result).

Cheers,
Malcolm

-- 
If it walks out of your refrigerator, LET IT GO!!




More information about the Python-list mailing list