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
john larson
16,594 PointsMy code passed this challenge, but it seems disjointed.
https://teamtreehouse.com/library/python-basics/letter-game-app/even-or-odd-loop
import random
start = 5
def even_odd(num):
return not num % 2
while start:
rn = random.randint(1, 99)
if rn % 2 == 0:
print("{} is even".format(rn))
else:
print("{} is odd".format(rn))
start -= 1
I looked at some other answers and realized I didn't even use the function. Makes me wonder why it was there at all. And if I was supposed to use the function, why did my code pass?
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsHey John,
I don't know If you already worked it out or not. A good way to think about it is like this.
while <some_condition>: # so while <some condition> is a True statement
# some_condition must be a TRUTHY value.
# NON-Empty List, True, NON-Empty String will make this run.
while not <some_condition>: # while <this condition> is anything but a True statement
# some_condition can equal None, False, empty string, or empty list and this will run.
So another way you could approach it is being more explicit about what you are looking for.
same = False # given this variable
while same == False: # the condition is (same == False ) which evaluates to True
# this would run
while same == True: # This would eval to False and wouldnt run.
# because same equals False. And False is not equal to True.
Go play around with those, youll start to see whats happening. :)
john larson
16,594 PointsThanks again Chris. This line...
while same == False: # the condition is (same == False ) which evaluates to True
Really cleared it up for me. I knew I had seen False used in the conditional part of a while, but I couldn't get it to work. Now I can see why. That inversion thing. Same is False, but it's True that same is equal to false. Got it :D
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsBecause it is the Python Basics series I believe they gave you the function to start you out with something. Mainly to teach you to read pre-existing code and learn to use what is already there.
The reason your code passed:
import random
start = 5
def even_odd(num):
return not num % 2 # This line is the same as check below.
while start:
rn = random.randint(1, 99)
if rn % 2 == 0: # Does the same thing as the function above
print("{} is even".format(rn))
else:
print("{} is odd".format(rn))
start -= 1
You basically went the long way and rewrote the functionality part of that function. And they could of made this fail, doing extra validation that you used their implemented function. But technically its not wrong that you went the longer way doing the same thing.
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsIn programming you will find there is almost always another way to rewrite something.
Sometimes the others ways can be the verbose way, which can be good if the code is already very cryptic. Sometimes the other ways are less verbose but much more readable, take up less lines, and just look awesome.
It really is scenario based, if you work on a team and that entire team knows those less verbose lines. Then I would stick with it. Because less code is nice.
If you are trying to explain a piece of functionality to someone, sometimes more explicit, more verbose way. It is easier to see what is happening.
In responding to this it made me think of The Zen of Python
john larson
16,594 Pointsjohn larson
16,594 PointsThanks Chris, I appreciate your response and I'm gonna look at that Python zen link. While I got you here... I have this code that works one way, but if I change the while condition to what looks like it would work, it never runs. Have a look if you have time.