Floating point calculation problem

Chris Angelico rosuav at gmail.com
Sat Feb 2 11:34:42 CET 2013


On Sat, Feb 2, 2013 at 9:27 PM, Schizoid Man <schiz_man at 21stcentury.com> wrote:
> The quantity s is input with the following line: s = input("Enter s:   ")
>
> To get rid of the compile error, I can cast this as a float: s =
> float(input("Enter s:   "))
>
> However, then the result returned by the method is wrong. Why does this
> error occur in version 3.3.0 but not in 2.7.3? Why is the result incorrect
> when s is cast as a float (the casting is not required in 2.7.3)? How is
> Python dynamically typed if I need to cast (in version 3.3.0 at least) to
> get rid of the compile error?

Did you use input() or raw_input() in 2.7.3? If the former, you were
actually doing this:

s = eval(input("Enter s:  "))

That's extremely dangerous and inadvisable, so it's better to go with
3.3 or the raw_input function.

Passing it through float() is, most likely, the right way to do this.
But what do you mean by "the result... is wrong"? That's the bit to
look into.

ChrisA



More information about the Python-list mailing list