[Tutor] Rounding Error

Michael Lewis mjolewis at gmail.com
Sat Jan 28 06:55:43 CET 2012


I am trying to round a float to two decimals, but I am getting the
following error:

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    PaintingProject()
  File "C:/Python27/Homework/Labs/Lab 03_5.py", line 42, in PaintingProject
    print 'That will cost you $%f.' %(round(5.6523),2)
TypeError: not all arguments converted during string formatting

Basically, I am writing a program to ask a user how many square feet they
need to paint. I then calculate how many cans of paint they need and will
also show the total purchase price. I've tried this two ways, and the first
way I am semi-successful meaning that my output is rounding but it displays
a number of zero's after the number rounds. The second way, I am getting
the error mentioned above. What am I doing wrong? How can I do this better?
(note, for the sake of brevity, I only show the code in question and not
the whole program).

        total = PRICE_PER_CAN * cans + (PRICE_PER_CAN * cans * TAX)
        total = round(total,2)
        print 'For %d square feet, you\'ll need %d cans of paint.'
%(square_feet, cans)
        print 'That will cost you $%f.' %(total)



        total = PRICE_PER_CAN * cans + (PRICE_PER_CAN * cans * TAX)
        print 'For %d square feet, you\'ll need %d cans of paint.'
%(square_feet, cans)
        print 'That will cost you $%f.' %(round(total),2)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120127/268949e4/attachment.html>


More information about the Tutor mailing list