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 PointsIs this snippet of code common in Python?
Or is it more of a specific bit of logic to solve this particular situation:
# fruit is randomly chosen from a list
# good_guess, bad_guess are lists holding respective guesses
def draw(fruit, good_guess, bad_guess):
for letter in fruit: # loop over each letter in fruit
if letter in good_guess: # if that letter is in the respective list
print(letter, end="") # it will be printed at that same index
else:
print("_", end="") # if no letter matches that index a _ will be printed
# result will be something like _pp__ (for apple when p is guessed)
I find this snippet particularly clever and I'm wondering if it's common or if Kenneth came up with it just to solve this situation. Thank :D
1 Answer
Steven Parker
243,318 Points
I might say this is a common approach to programming situations.
It's a Python-specific implementation of a common language-independent approach to this kind of problem. That doesn't diminish it's cleverness. Recognizing where and how to apply common logic is an essential part of clever programming.
john larson
16,594 Pointsjohn larson
16,594 PointsThanks Steve.