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 trialNoor Elharty
2,630 PointsMy modification for prompt_for_words function
i modified the prompt_for_ words so it does :
- only accept strings with two or more letters
- only accept strings with letters within the range of letters in the given random word
- only accept strings with number of letters equal or below number of letters of the given random word
def prompt_for_words(challenge):
guesses = set()
print("What words can you find in word '{}'".format(challenge))
print("(Enter Q to Quit)")
while True:
try:
guess = input("{} words > ".format(len(guesses)))
if guess.upper() == 'Q':
break
if set(list(guess.lower())) <= set(list(challenge.lower())) and len(guess) >= 2 \
and len(guess) <= len(challenge):
print("BINGO!")
else:
raise AssertionError()
guesses.add(guess.lower())
except AssertionError:
print("Oops! Try again :) ")
return guesses
What are your thoughts ? and how can I improve it a bit further ? like how to make it accept strings with the exact number of letters within letters of the given word ? example: (right now ---> random word = word -----> it accepts guess = wowo , even there are only one 'o' and one 'w' in random word)
1 Answer
amyyxt
2,227 PointsHi Noor,
There are a couple of things I want to point out (enumerated below), but they both point toward not explicitly using word length and sets of word letters as criteria for checking word validity. Instead, try comparing a list of the letters in the guess word against a list of the letters in the challenge word.
Another question associated with the "Out of This Word" video and its answers use this latter method instead. One of the answers suggests using sets, but another answer brings up a good argument against using sets, which I mention briefly below. Of course, even this latter method cannot check whether the guess is a "real" word that someone might find in a dictionary, whether or not it includes slang.
Some words are less than two letters long. For example, "a" and "I" are valid words. While the letter "i" isn't in any of the provided challenge words, "a" is in one of them: "learner".
What if the length of a guess word is less than the length of challenge word and all the letters in the set of letters in the guess word appear in the set of letters in the challenge word, but one of the letters in the guess word repeats and that letter does not repeat in the challenge word? For example, guessing "all" would pass your tests if the challenge word were "learner." However, we don't want "all" to pass because it has two "l"'s even though "learner" has only one.
URL to the discussion I reference: https://teamtreehouse.com/community/out-of-this-word-check-for-valid-words-is-this-the-best-way