[issue44866] Inconsistent Behavior of int()

Steven D'Aprano report at bugs.python.org
Sun Aug 8 15:02:33 EDT 2021


Steven D'Aprano <steve+python at pearwood.info> added the comment:

John, there's no need to establish every feature in Python that you are **not** questioning. Please focus on the behaviour that you think is a bug. Pretty much nothing in your example before the line "So everything here is working as expected" is necessary. We're experienced Python users, some of us have been using Python for 20 years or more. We know how int() operates, you don't have to teach us.

What you have done is confuse the string '2.8' and the float 2.8, which is easy enough to do as a beginner. The critical error is this line:

    int_y = int(2.8) #conver y to an integer 2 and assign to int_y

But that's not converting the variable `y` to an integer, it is converting the float 2.8 to an integer. 2.8 is already a number, and calling int(2.8) drops the fractional part leaving 2 as expected.

Had you tried `int_y = int(y)` instead (y being a string) you would have got the exact error you were expecting.

It takes a little while to completely understand the differences between strings, ints, floats, literal numbers like 2.8 versus strings like "2.8", but that will come with some practice.

In future, rather than posting on the bug tracker, please consider asking questions on one of the many forums where volunteers will answer your questions and leave the bug tracker for actual bugs.

(Hint: there are many tens of thousands of Python programmers with collectively thousands of years of experience with the language. As a beginner with only a few days experience, you are highly unlikely to spot a bug that everyone else has missed.)

You can try the Python Discussion area:

https://discuss.python.org/


or the Python-List or Tutor mailing lists:

https://mail.python.org/mailman/listinfo/python-list

https://mail.python.org/mailman/listinfo/tutor


Good luck!

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44866>
_______________________________________


More information about the Python-bugs-list mailing list