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
pegamoose
16,046 PointsProblem 3: Missbuzz - Kenneth does the opposite
The instructions say to print out the number if it is divisible by 3 or 5 or both, not to skip it. So why is Kenneth skipping these?
Problem 3: Missbuzz
Write a while loop that increments current by 1
If the new number is divisible by 3, 5, or both,
print out the number. Otherwise, skip it.
Break out of the loop when current is equal to 101.
2 Answers
Dave StSomeWhere
19,870 PointsI don't think Kenneth was skipping number divisible by 3 or 5 (or both). I'm guessing that it is his solution that is confusing you, and probably the if not... statement.
Here's his code:
current = 1
while current < 101:
if not current % 3 or current % 5 == 0:
print(current)
current += 1
Kenneth does explain this, so it might be worth watching again. He's just demonstrating two different ways to accomplish the same thing (one way on the divisible by 3 and one way on the divisible by 5)
An if statement evaluates an expression to true or false. When there is an or then either part can evaluate to true to evaluate the entire if statement to true.
Part 1 is - not current % 3 - when current is divisible by 3, current % 3 is equal to zero which is false and by putting the not in front of it evaluates to true. Remember the not only applies to one side of the or. Any number divisible will result in a Truthy result and a the If evaluates to true.
Part 2 is - 'current % 5 == 0` - which is basically the same as above, just another way to do it and I'm guessing that Kenneth is just showing both for the sake of demonstration and wouldn't mix the two methods in real life.
Does that answer your question?
pegamoose
16,046 PointsAh, thank you. His code seems a bit convoluted but your explanation helps.
pegamoose
16,046 PointsHere's the video link: https://teamtreehouse.com/library/the-solution-while-loops
KRIS NIKOLAISEN
54,974 PointsKRIS NIKOLAISEN
54,974 PointsCan you provide a link to the video?