Question
When the tip()
function is implemented according to the provided hint in this exercise, the values returned by the function differ if the code is executed using Python 2 versus Python 3. Why is that?
Answer
The difference is in the handling of division between the two version of Python. The hint states that the percentage should be divided by 100
. In Python 3, division is done using floating point math which preserves any fractional portion. In Python 2, division is done using integer math which drops any fractional component unless one of the things being divided is a floating point number. To make the code equivalent between the two versions, the percentage should be divided by 100.0
or float(100)
to ensure that the division is done correctly in Python 2.