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 trialMateusz Cybulski
2,469 PointsWhy doesn't the while loop keep going?
For this video, the first example he is doing for while loops he subtracts 1 from 10 every time, why doesn't Python subtract it 1 from 10 continually, nothing is stopping it...
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsHe actually subtracts 1
from whatever the value of the variable start
is... which is only 10
at the beginning, then of course it is being reduced by 1
each time through the loop, and the loop stops when it reaches a 'falsey' value, such as 0
(zero).
The part to note is that he is using the -=
operator, which is short for 'equals the variable being assigned (left side), minus the value (right side)', i.e.
start -= 1
# same as:
start = start - 1
Abraham Juliot
47,353 PointsThe loop will only run while start is truthy. When start equals zero (a falsy value), the loop will end.
start = 10
while start:
print(start)
start -= 1
More on values that are considered false
Moderator edited: Moved response to Answers