1

HELP Im currently using a book, but it seems like its wrong or something. This is what I enter:

round(10)
10
>>> round(10.0)
10
>>> round(10.2)
10
>>> round(8.7)
9
>>> round(4.5, 1)
4.5
>>> round(4.5, 2)
4.5
>>> round(4.5, 3)
4.5
>>> round (4.5)
4
>>> round(4.5)
4

Here is what the book says should happen:

round(10)
10
>>> round(10.0)
10.0
>>> round(10.2)
10.0
>>> round(8.7)
9.0
>>> round (4.5)
5
coldspeed95
407k106 gold badges745 silver badges798 bronze badges
asked Jun 21, 2017 at 19:04
7
  • 4
    You are probably using Python 3, and the book is for Python 2. Commented Jun 21, 2017 at 19:07
  • Forget the book. Look at what round is supposed to do...You can get the behavior described by the book by supplying the number of significant digits. round(10.0, 1) --> 10.0. The docs say "digits (default 0 digits)." NOTE: This is for Python 2.7x Commented Jun 21, 2017 at 19:07
  • @LOL, LOL...... Commented Jun 21, 2017 at 19:08
  • Was the book for python 2 or python 3? Were you using python 2 or 3? My guess - the book was written for python 2 and you are using python 3. Commented Jun 21, 2017 at 19:08
  • Actually, we are both using the same version of python... Commented Jun 21, 2017 at 19:09

1 Answer 1

1

Python2.7

>>> round(10.0)
10.0
>>> round(10.0, 0)
10.0

Python3.4, 3.5, and 3.6

>>> round(10.0)
10
>>> round(10.0, 0)
10.0
>>> 

Two takeaways:

  1. round in python3 returns an int (when it can) while in python2 returns float (always).
  2. Read a book for python3!
answered Jun 21, 2017 at 19:09
Sign up to request clarification or add additional context in comments.

3 Comments

For me round(10.0) is returning 10. Using Python3.6.1
Ok...im using 3.5.1, is that it?
@LOL Can confirm, 3.5 shows the same behaviour. Now you can safely assume you're reading the wrong book. Cheers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.