Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 why it's not printing?
#1
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
Quote
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
thank you sir.
Quote
#4
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 .
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  printing a bytes string Skaperen 2 98 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  Printing lists acooper7 1 168 Jun-17-2019, 09:37 PM
Last Post: Yoriz
  Printing List in one line bharat_s579 6 317 May-26-2019, 08:30 PM
Last Post: perfringo
  list is printing incorrectly.. anna 1 171 May-18-2019, 12:12 PM
Last Post: ichabod801
  no outcome is printing helpme 2 238 Mar-07-2019, 02:15 PM
Last Post: helpme
  Printing Easter date occurrences samsonite 8 467 Mar-06-2019, 11:49 AM
Last Post: samsonite
  dict printing last key,value out of loop anna 6 390 Feb-23-2019, 03:22 AM
Last Post: anna
  tuple printing trouble anna 0 245 Feb-09-2019, 05:24 PM
Last Post: anna
  Printing one thing from a list bidoofis 1 294 Feb-05-2019, 09:02 PM
Last Post: ichabod801
  printing maxd = sr.iloc[:1, 1:2] gehrenfeld 3 271 Feb-04-2019, 12:28 PM
Last Post: gehrenfeld

Forum Jump:


Users browsing this thread: 1 Guest(s)