This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
intro = str(len(secretWord)) | |
mistakesMade = 8 | |
lettersGuessed = [] | |
guess = str | |
availableLetters = str(getAvailableLetters(lettersGuessed)) | |
print 'Welcome to the game, Hangman!' | |
print ('I am thinking of a word that is ') + intro + (' letters long.') | |
if mistakesMade >= 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 6.00 Problem Set 3 | |
# | |
# Hangman game | |
# | |
# ----------------------------------- | |
# Helper code | |
# You don't need to understand this helper code, | |
# but you will have to know how to use the functions | |
# (so be sure to read the docstrings!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
count = 0 | |
newHand= str(hand) | |
if count <= len(word): | |
for c in word: | |
if c in newHand: | |
count += 1 | |
else: | |
return False | |
if word in wordList: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Keep track of two numbers: the number of letters left in your hand and the total score | |
score = 0 | |
numbersLeft = int(calculateHandlen(hand)) | |
# As long as there are still letters left in the hand: | |
while numbersLeft > 0: | |
# Display the hand | |
print 'Current Hand: ', | |
displayHand(hand) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def playGame(wordList): | |
""" | |
Allow the user to play an arbitrary number of hands. | |
1) Asks the user to input 'n' or 'r' or 'e'. | |
* If the user inputs 'n', let the user play a new (random) hand. | |
* If the user inputs 'r', let the user play the last hand again. | |
* If the user inputs 'e', exit the game. | |
* If the user inputs anything else, tell them their input was invalid. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compChooseWord(hand, wordList): | |
""" | |
Given a hand and a wordList, find the word that gives | |
the maximum value score, and return it. | |
This word should be calculated by considering all the words | |
in the wordList. | |
If no words in the wordList can be made from the hand, return None. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
def buildCoder(shift): | |
""" | |
Returns a dict that can apply a Caesar cipher to a letter. | |
The cipher is defined by the shift value. Ignores non-letter characters | |
like punctuation, numbers, and spaces. | |
shift: 0 <= int < 26 | |
returns: dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Problem 2: Decryption | |
# | |
def findBestShift(wordList, text): | |
""" | |
Finds a shift key that can decrypt the encoded text. | |
text: string | |
returns: 0 <= int < 26 | |
""" | |
#1. Set the maximum number of real words found to 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TODO: WordTrigger | |
class WordTrigger(Trigger): | |
def __init__(self, word): | |
self.word = word.lower() | |
def isWordIn(self, text): | |
newtext=[] | |
self.text = text.lower() | |
for c in string.punctuation: | |
self.text = self.text.replace(c, ' ').lower() | |
newText = self.text.split(' ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enter your code for WordTrigger, TitleTrigger, | |
class WordTrigger(Trigger): | |
def __init__(self, word): | |
self.word = word.lower() | |
def isWordIn(self, text): | |
newtext=[] | |
self.text = text.lower() | |
for c in string.punctuation: | |
self.text = self.text.replace(c, ' ').lower() | |
newText = self.text.split(' ') |
OlderNewer