Sqyer
Sqyer

Reputation: 83

Unexpected mathematical output

I am trying to count my eggs in an exercise in Learning Python the Hard Way. The formula for counting the eggs is:

print (3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)

and the suggested answer is 7. I am getting 6.75 and have no idea why, I think I am putting it in correctly but I could be wrong. The way it is shown above is exactly how I have put it into the program.

Note: The book I am using uses the 2.6 version of Python where I am using the 3.1 version. This might be part of the confusion. Please help.

The URL for reference here.

Upvotes: 2

Views: 161

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177406

Python 3 is non-backwards-compatible with Python 2. Use the version of Python that your book is based on for least confusion. As others have mentioned, Python 3 and Python 2 perform division differently, among other things.

Upvotes: 0

Ismail Badawi
Ismail Badawi

Reputation: 37167

In python 2.x, the / operator did integer division. In python 3, the result of the / is a float regardless of the input types. Use the // operator to perform integer division.

Upvotes: 12

Related Questions