Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What am I not understanding from %?

For those familiar with the book, I'm doing ex3.py from Learn Python the Hard Way. My code is fine and dandy (minus the floating points, but that's for another topic) but what I can't seem to comprehend is %. I've never been very good at math and can't wrap my head around it but I'm hoping that someone could dissect the logic in this fairly similar but different equation.

Why is it that 100 - 25 * 6 % 4 = 98 Why not 95? 100 - ((25*6) % 4) 100 - ((150) % 4) If the remainder of 150 / 4 is 5 how is it that I'm getting 98?

What am I missing?

Robert Uhler
Robert Uhler
3,637 Points

Not quite sure how you are getting to 98 or 95 but the first problem is your calculation of the remainder. 150 % 4 is not 5 but 2. The remainder is 2. Perhaps this is where you issues lies. 150/ 4 == 37 R2

1 Answer

Steven Parker
Steven Parker
231,275 Points

The "remainder" is what is left over after division. It will always be smaller than the operand. So "anything % 4" can never be larger than 3, and certainly not 5.

Here are some examples:

0 % 4 = 0          148 % 4 = 0
1 % 4 = 1          149 % 4 = 1
2 % 4 = 2          150 % 4 = 2
3 % 4 = 3          151 % 4 = 3
4 % 4 = 0          152 % 4 = 0

Oh man thanks, I finally get it. I was going above and beyond from what I was meant to do.

So to simplify things, the first remainder I get is what I'm meant to take?