Search code examples
pythonpython-3.xcounterdivisionrounding-error

Rounding, variable from a .txt in Python 3 and extra


if op in ["/"]:
    # rounding does not rounds half up so I imported a module
    from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_DOWN
    Decimal(str(answer)).quantize(Decimal("1.11"), rounding=ROUND_HALF_UP)
    Decimal(str(given_answer)).quantize(Decimal("1.11"), rounding=ROUND_HALF_UP)
print("correct {}".format(answer, ".2f"))

49//34=?1.44

correct 1

Wrong answer!

I want it to have the right answer as 3.56 if the real answer is 3.55865465. What am I doing wrong?

Written text file with python


Solution

  • Try changing the last line to the following:

    print("correct {:.2f}".format(answer))