remove .0 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: remove .0 (/thread-29999.html) |
remove .0 - oli_action - Sep-29-2020 Hey everyone. I've written this code. It all works fine except the answer always has a .0 . The answer is correct, but is there a way to get rid of the .0? Thanks number_1 = 1 number_2 = 1 enter = int(input('Please enter first number: ')) value = enter while value > 0: number_1 = number_1 * value value = value - 1 print('Intermediate result 1:', number_1) enter_2 = int(input('Please enter second number: ')) sub = enter - enter_2 value = sub while value > 0: number_2 = number_2 * value value = value - 1 print('Intermediate result 2:', number_2) print('The Answer is:', number_1 / number_2)
RE: remove .0 - scidam - Sep-29-2020 You can convert the result to integer data type, int(number_1 / number_2) . Also, you can just use integer division, e.g. number_1 // number_ 2 .
RE: remove .0 - oli_action - Sep-29-2020 Hello scidam. Thanks for the quick reply. Both are cool. Would you recommend to do this though or is it just a matter of preference? RE: remove .0 - perfringo - Sep-29-2020 I suggest to learn concept of datatypes. There are built in numeric datatypes: int, float and complex. So using int() to 'remove .0' is not removing at all but converting. Beware of this expected behaviour while converting: >>> int(5.9) # for floating point numbers truncates towards zero; this is not rounding 5 // (floor division) behaviour: >>> 5 // 2 2 >>> 4 // 2 2So if there is any chance that you may end up with not integer value as a result of division you will not get correct result with both int() and //. ...and I don't want to open Pandora's box of floating point representation precision in binary |