[Tutor] Newbie - mixing floats and integers (first post)
Adam
adam at monkeez.org
Mon Apr 12 15:37:55 EDT 2004
On Mon, 12 Apr 2004 11:45:54 -0700 (PDT)
Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
> On Sun, 11 Apr 2004, Adam wrote:
>
> > Just picking up python with Learning Python (on day 3
> > now - be kind) and have written my own small calculator
> > script:
> >
> > arg1 = raw_input("What is your first number?:")
> > oper = raw_input("What operation: +, -, *, / ?")
> > arg3 = raw_input("What is your second number?:")
> >
> > num1 = long(arg1)
> > #oper = arg [2]
> > num2 = long(arg3)
> >
> > if oper == "+":
> > answer = num1+num2
> > elif oper == "-":
> > answer = num1-num2
> > elif oper == "/":
> > answer = (num1/num2)
> > elif oper == "x":
> > answer = (num1*num2)
> >
> > print num1, oper, num2, "=", answer
> >
> > I've read in learning Python that the variable types
> > look after themselves - "great" I think, less hassle
> > than Java.
> >
> > However, when I try and run this program with floats, it
> > won't work and I get errors. Is there something I have
> > to do to enable this to work with both floats and
> > integers?
>
>
> Hi Adam,
>
>
> Yes, there's a way to do it.
>
<snip>
>
> One way to fix the program is to call either int() or
> float(), conditionally based on the content of the string.
> Here's one approach: if
> the string contains a decimal point, we can call our float
> conversion routine. And otherwise, we can call our
> integer-converting routine:
>
> ###
> >>> def num(s):
> ... """Returns either an integer or a float out of
> string 's'."""... if '.' in s:
> ... return float(s)
> ... else:
> ... return int(s)
> ...
> >>> num("3.1415926")
> 3.1415926000000001
> >>> num("42")
> 42
> ###
>
> You can use this 'num()' function to do the
> string-to-numeric conversions, and it should do the right
> thing, for the most part. *grin*
>
>
>
>
> The problem is very similar to what happens in Java when
> we use Java's Integer.parseInt() function on a string that
> looks like a floating-point number:
>
> ###
> [dyoo at tesuque dyoo]$ jython
> Jython 2.1 on java1.4.1_01 (JIT: null)
> Type "copyright", "credits" or "license" for more
> information.
> >>> import java
> >>> java.lang.Integer.parseInt("3.1415926")
> Traceback (innermost last):
> File "<console>", line 1, in ?
> java.lang.NumberFormatException: For input string:
> "3.1415926"
> at
> java.lang.NumberFormatException.forInputString(NumberForm
> atException.java:48)
>
> [... long Java traceback cut]
> ###
>
> (I'm using Jython --- a Java/Python bridge --- to show
> what things look like on the Java side. It's a very nice
> tool if you have Java experience.)
>
> So the conversion issues between strings and the primitive
> types are also similar in Java --- the string-to-integer
> routines on both are a bit strict.
> Please feel free to ask more questions. Hope this helps!
Danny,
Many thanks for your reply - very helpful stuff (much more
helpful than the Java forums). I really liked the idea of
checking for a '.' and then casting appropriately (does
casting mean anything in python)? This seems like a clean
solution and most appropriate for both conditions (int and
float).
I'm also glad to report that my experience here (albeit a
short one) is much more positive than the brief experiences
I had within the java help community (although, I imagine
that they are normally helpful). I'm afraid, it looks like
I'll be staying :-)
Thanks once again.
adam
More information about the Tutor
mailing list