Pset2file - Jupyter Notebook
Pset2file - Jupyter Notebook
localhost:8888/notebooks/Documents/ocw/pset2file.ipynb 1/6
11/1/22, 5:51 PM pset2file - Jupyter Notebook
localhost:8888/notebooks/Documents/ocw/pset2file.ipynb 2/6
11/1/22, 5:51 PM pset2file - Jupyter Notebook
def get_available_letters(letters_guessed):
'''
letters_guessed: list (of letters), which letters have been guessed so far
returns: string (of letters), comprised of letters that represents which letters have not
yet been guessed.
'''
# FILL IN YOUR CODE HERE AND DELETE "pass"
alphabet = 'abcdefghijklmnopqrstuvwxyz'
not_guessed = ''
for i in alphabet:
if i not in letters_guessed:
not_guessed += i
return not_guessed
def hangman(secret_word):
'''
secret_word: string, the secret word to guess.
* At the start of the game, let the user know how many
letters the secret_word contains and how many guesses s/he starts with.
* Ask the user to supply one guess per round. Remember to make
sure that the user puts in a letter!
for i in secret_word[::]:
if i not in unique:
unique.append(i)
unique_letters_in_secret_word = len(unique)
print ('------------')
while guesses_remaining > 0 and warnings_remaining > 0:
print ('You have' + ' ' + str(guesses_remaining) + ' ' + 'guesses left.')
print ('Available letters: ' + get_available_letters(letters_guessed))
guess = input('Please guess a letter: ').lower()
guess_is_alpha = guess.isalpha()
if not guess_is_alpha:
warnings_remaining -= 1
print ("Oops! That isn't a letter. You have " + ' ' + str(warnings_remaining) + ' ' +'warnings le
continue #loops it back up to the start
elif guess in secret_word and guess not in letters_guessed:
letters_guessed += guess
good_guess += 1
print ('Good guess:' + get_guessed_word(secret_word, letters_guessed))
elif guess in letters_guessed:
warnings_remaining -= 1
print ("Oops! You've already guessed that letter. You have " + ' ' + str(warnings_remaining) + '
else:
letters_guessed += guess
print ('Oops! That letter is not in my word: ' + get_guessed_word(secret_word, letters_guessed))
if guess in vowels:
guesses_remaining -= 2
else:
guesses_remaining -=1
print ('------------')
if is_word_guessed(secret_word, letters_guessed):
print('Congratulations, you won!')
score = guesses_remaining * unique_letters_in_secret_word
print('Your total score for this game is: ' + str(score))
localhost:8888/notebooks/Documents/ocw/pset2file.ipynb 3/6
11/1/22, 5:51 PM pset2file - Jupyter Notebook
break
if warnings_remaining == 0:
print ('Sorry, you ran out of warnings. The word was ' + secret_word + '.')
if guesses_remaining == 0:
print ('Sorry, you ran out of guesses. The word was ' + secret_word + '.')
# When you've completed your hangman function, scroll down to the bottom
# of the file and uncomment the first two lines to test
#(hint: you might want to pick your own
# secret_word while you're doing your own testing)
# -----------------------------------
def match_with_gaps(my_word: str, other_word: str) -> bool:
'''
my_word: string with _ characters, current guess of secret word
other_word: string, regular English word
returns: boolean, True if all the actual letters of my_word match the
corresponding letters of other_word, or the letter is the special symbol
_ , and my_word and other_word are of the same length;
False otherwise:
'''
if len(my_word) != len(other_word):
return False
else:
import re
strs = my_word
other_word_clone = other_word
underscore_positions = [x.start() for x in re.finditer('\_', strs)]
for i in underscore_positions:
other_word_clone = other_word_clone[0:i] + '_' + other_word_clone[i + 1:]
if other_word_clone == my_word:
return True
return False
# my_word_stripped = my_word.replace(" ", "")
# same_char = []
# blank_stripped = []
# if len(my_word_stripped) != len(other_word):
# return False
# for index, letter in enumerate(my_word_stripped):
# if letter in string.ascii_lowercase:
# same_char.append(index)
# else:
# blank_stripped.append(index)
# mws = ''
# ow = ''
# for index_same in same_char:
# for index_dif in blank_stripped:
# if other_word[index_dif] == other_word[index_same]:
# return False
# mws += my_word_stripped[index_same]
# ow += other_word[index_same]
# return mws == ow
def show_possible_matches(my_word):
'''
my_word: string with _ characters, current guess of secret word
returns: nothing, but should print out every word in wordlist that matches my_word
Keep in mind that in hangman when a letter is guessed, all the positions
at which that letter occurs in the secret word are revealed.
Therefore, the hidden letter(_ ) cannot be one of the letters in the word
that has already been revealed.
'''
# possible_matches = list()
# for i in wordlist:
# if match_with_gaps(my_word, i):
# possible_matches.append(i)
# return spm
possible_matches = [i for i in wordlist if match_with_gaps(my_word, i)]
return ' '.join(possible_matches)
def hangman_with_hints(secret_word):
'''
secret_word: string, the secret word to guess.
* At the start of the game, let the user know how many
letters the secret_word contains and how many guesses s/he starts with.
* Before each round, you should display to the user how many guesses
s/he has left and the letters that the user has not yet guessed.
* Ask the user to supply one guess per round. Make sure to check that the user guesses a letter
* If the guess is the symbol *, print out all words in wordlist that
matches the current guessed word.
for i in secret_word[::]:
if i not in unique:
unique.append(i)
unique_letters_in_secret_word = len(unique)
print ('------------')
while guesses_remaining > 0 and warnings_remaining > 0:
print ('You have' + ' ' + str(guesses_remaining) + ' ' + 'guesses left.')
print ('Available letters: ' + get_available_letters(letters_guessed))
guess = input('Please guess a letter: ').lower()
guess_is_alpha = guess.isalpha()
if guess == '*':
print("Possible matches are:", show_possible_matches(get_guessed_word(secret_word, letters_guesse
elif not guess_is_alpha:
warnings_remaining -= 1
print ("Oops! That isn't a letter. You have " + ' ' + str(warnings_remaining) + ' ' +'warnings le
continue #loops it back up to the start
elif guess in secret_word and guess not in letters_guessed:
letters_guessed += guess
good_guess += 1
print ('Good guess:' + get_guessed_word(secret_word, letters_guessed))
elif guess in letters_guessed:
warnings_remaining -= 1
print ("Oops! You've already guessed that letter. You have " + ' ' + str(warnings_remaining) + '
else:
letters_guessed += guess
print ('Oops! That letter is not in my word: ' + get_guessed_word(secret_word, letters_guessed))
if guess in vowels:
guesses_remaining -= 2
else:
guesses_remaining -=1
print ('------------')
if is_word_guessed(secret_word, letters_guessed):
print('Congratulations, you won!')
score = guesses_remaining * unique_letters_in_secret_word
print('Your total score for this game is: ' + str(score))
break
if warnings_remaining == 0:
print ('Sorry, you ran out of warnings. The word was ' + secret_word + '.')
if guesses_remaining == 0:
print ('Sorry, you ran out of guesses. The word was ' + secret_word + '.')
# When you've completed your hangman_with_hint function, comment the two similar
# lines above that were used to run the hangman function, and then uncomment
# these two lines and run this file to test!
# Hint: You might want to pick your own secret_word while you're testing.
if __name__ == "__main__":
# pass
# To test part 2, comment out the pass line above and
localhost:8888/notebooks/Documents/ocw/pset2file.ipynb 5/6
11/1/22, 5:51 PM pset2file - Jupyter Notebook
# uncomment the following two lines.
# secret_word = choose_word(wordlist)
# hangman(secret_word)
###############
secret_word = choose_word(wordlist)
hangman_with_hints(secret_word)
In [ ]:
In [ ]:
localhost:8888/notebooks/Documents/ocw/pset2file.ipynb 6/6