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 trialAhmet GULER
7,181 Pointswhat does it means 'while variable:' ?
Hello,
As start is a variable here. and there is no condition, i am confused. What does it means:
while start:
does it means ? while a variable named as 'start' exits? or while start == True or while start == False
So how many times this loop will iterate? what will make the condition false and break the loop ?
import random
def even_odd(num):
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
return not num % 2
start = 5
while start:
number = random.randint(1,99)
if even_odd(number) == True:
print("{} is even".format(number))
else:
print("{} is odd".format(number))
start -= 1
5 Answers
Carlos Federico Puebla Larregle
21,074 PointsIt means that, while the variable "start" is "truthy" is going to execute the code inside that scope. If you notice, the variable start is decremented inside the while scope
start -= 1
The while condition is going to be true until "start" is equal to 0. 0 falsy value any other number is a truthy value.
I hope that clarifies it a little bit.
Ahmet GULER
7,181 Pointsthanks. great!
Ahmet GULER
7,181 PointsI just have one more small question. then why doesn't it work when i replace the statement as:
while start == True
Carlos Federico Puebla Larregle
21,074 PointsBe cause Python does not make "Type coercion". This means that when the variable "start" is equal to the number 5 is "truthy" but is not actually the boolean value "True".
Ahmet GULER
7,181 Pointsok. thanks for your quick and kind help.
Carlos Federico Puebla Larregle
21,074 PointsYou're Welcome Ahmet. Any time.