Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 297923e

Browse files
Create PSet_hangman
1 parent 33fb3c0 commit 297923e

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

‎MIT6.0.0.1X/week_3/PSet_hangman

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Hangman game
2+
3+
import random
4+
5+
WORDLIST_FILENAME = "words.txt"
6+
7+
8+
def loadWords():
9+
"""
10+
Returns a list of valid words. Words are strings of lowercase letters.
11+
12+
Depending on the size of the word list, this function may
13+
take a while to finish.
14+
"""
15+
print("Loading word list from file...")
16+
# inFile: file
17+
inFile = open(WORDLIST_FILENAME, 'r')
18+
# line: string
19+
line = inFile.readline()
20+
# wordlist: list of strings
21+
wordlist = line.split()
22+
print(" ", len(wordlist), "words loaded.")
23+
return wordlist
24+
25+
26+
def chooseWord(wordlist):
27+
"""
28+
wordlist (list): list of words (strings)
29+
30+
Returns a word from wordlist at random
31+
"""
32+
return random.choice(wordlist)
33+
34+
35+
# Load the list of words into the variable wordlist
36+
# so that it can be accessed from anywhere in the program
37+
wordlist = loadWords()
38+
39+
40+
def isWordGuessed(secretWord, lettersGuessed):
41+
'''
42+
secretWord: string, the word the user is guessing
43+
lettersGuessed: list, what letters have been guessed so far
44+
returns: boolean, True if all the letters of secretWord are in lettersGuessed;
45+
False otherwise
46+
'''
47+
return all(x in lettersGuessed for x in secretWord)
48+
49+
50+
def getGuessedWord(secretWord, lettersGuessed):
51+
'''
52+
secretWord: string, the word the user is guessing
53+
lettersGuessed: list, what letters have been guessed so far
54+
returns: string, comprised of letters and underscores that represents
55+
what letters in secretWord have been guessed so far.
56+
'''
57+
newlist = []
58+
secret_word_list = list(secretWord)
59+
for x in secret_word_list:
60+
if x in lettersGuessed:
61+
newlist.append(x)
62+
else:
63+
newlist.append('_ ')
64+
return ''.join(newlist)
65+
66+
def getAvailableLetters(lettersGuessed):
67+
'''
68+
lettersGuessed: list, what letters have been guessed so far
69+
returns: string, comprised of letters that represents what letters have not
70+
yet been guessed.
71+
'''
72+
import string
73+
alphabet_list = list(string.ascii_lowercase)
74+
for letter in lettersGuessed:
75+
if letter in alphabet_list:
76+
alphabet_list.remove(letter)
77+
alphabet_list = alphabet_list
78+
alphabet_string = ''.join(alphabet_list)
79+
return alphabet_string
80+
81+
def hangman(secretWord):
82+
'''
83+
secretWord: string, the secret word to guess.
84+
85+
Starts up an interactive game of Hangman.
86+
87+
* At the start of the game, let the user know how many
88+
letters the secretWord contains.
89+
90+
* Ask the user to supply one guess (i.e. letter) per round.
91+
92+
* The user should receive feedback immediately after each guess
93+
about whether their guess appears in the computers word.
94+
95+
* After each round, you should also display to the user the
96+
partially guessed word so far, as well as letters that the
97+
user has not yet guessed.
98+
99+
Follows the other limitations detailed in the problem write-up.
100+
'''
101+
counter = 8
102+
secret_word_list = list(secretWord)
103+
lettersGuessed = []
104+
print('Welcome to the game Hangman!')
105+
print('I am thinking of a word that is', str(len(secretWord)), 'letters long.')
106+
print('------------')
107+
while counter > 0:
108+
print('You have', counter, 'guesses left.')
109+
print('Available letters:', getAvailableLetters(lettersGuessed))
110+
print('Please guess a letter:')
111+
guess = input()
112+
guess_in_lower_case = guess.lower()
113+
if guess_in_lower_case not in lettersGuessed:
114+
if guess_in_lower_case not in secret_word_list:
115+
lettersGuessed.append(guess_in_lower_case)
116+
print('Oops! That letter is not in my word:', getGuessedWord(secretWord, lettersGuessed))
117+
counter -= 1
118+
elif guess_in_lower_case in secret_word_list:
119+
lettersGuessed.append(guess_in_lower_case)
120+
print('Good guess:', getGuessedWord(secret_word_list, lettersGuessed))
121+
if isWordGuessed(secretWord, lettersGuessed) == True:
122+
print('Congratulations, you won!')
123+
break
124+
elif guess_in_lower_case in lettersGuessed:
125+
print("Oops! You've already guessed that letter:", getGuessedWord(secretWord, lettersGuessed))
126+
if counter == 0:
127+
print('Sorry, you ran out of guesses. The word was', secretWord)
128+
return
129+
130+
secretWord = chooseWord(wordlist).lower()
131+
hangman(secretWord)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /