
Code
Score a Possible Message
Time to teach the computer to "read". We'll give each candidate a score: one point for every word that appears in a list of common words. Gibberish scores 0. Real messages score high.
words = ["the", "is", "map"]
text = "The map is here!"
for word in text.lower().split():
print(word.strip(".,!?"))
.lower()makesThematchthe..split()chops the text into words at the spaces..strip(".,!?")peels punctuation off the ends, sohere!becomeshere.
Then pick the candidate with the highest score. If two candidates tie, keep the one that came first. That way the program always gives the same answer.
Assignment
score(text, words)returns how many words intextare in the listwords. Repeats count each time:score("the the", ["the"])is2.best_guess(candidates, words)returns the candidate with the highest score, usingscore. On a tie, return the earlier one.
score(text, words)returns how many words intextare in the listwords. Repeats count each time:score("the the", ["the"])is2.best_guess(candidates, words)returns the candidate with the highest score, usingscore. On a tie, return the earlier one.
main.py Python sleeping
Press Run to try your code, then Check to test it.