Python Forum
why it's not printing? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: why it's not printing? (/thread-20524.html)



why it's not printing? - nickto21 - Aug-15-2019

Hey all,
Im working through "The Self-taught programmer" by Cory Althoff.
I'm doing the exercises at the end of chapter 2, and i'm having two difficulties.

1. I'm suppose to "create a program that divides two variables and prints the remainder." When I input
"100 % 15" into the shell, I get output of "10", which is what I wanted. But when I input the exact same into a file, and then run module, I just get a restart message in the shell. how do I get the file to print the remainder in the shell?

2. Why is "10 // 0.1" giving the answer "99.0" , but "100 // 1" is giving answer 100? Both should be 100, so why 99.0 then?

Thank you,
Steve


RE: why it's not printing? - ichabod801 - Aug-15-2019

1. If you are executing from a file, you need the print() function to display output. So print(100 % 15)

2. I'm not sure. I would guess it's a floating point error. Fractional numbers are not stored perfectly in Python (or most other languages). So I would guess 10 // 0.1 is giving something close to but not quite 100 before the integer truncation happens. However, 10 / 0.1 gives the correct answer of 100.0, so I'm not sure about that.


RE: why it's not printing? - nickto21 - Aug-15-2019

thank you sir.


RE: why it's not printing? - scidam - Aug-16-2019

Floor division (//) in Python is almost the same as divmod builtin-function.
You can see its implementation from here.
Since divmod(10, 0.1) = (99.0, 0.0999999....etc) due to precision loss (as @ichabod801 mentioned above), you get 10//0.1=99.0 .