|
1 | 1 | import random # import random library
|
2 | 2 |
|
| 3 | + |
3 | 4 | def pick_random_word():
|
4 |
| - """ |
5 |
| - This function picks a random word from the SOWPODS |
6 |
| - dictionary. |
7 |
| - """ |
8 |
| - # open the sowpods dictionary as a text file in readable format |
9 |
| - # for more information on opening files visit https://pythontips.com/2014/01/15/the-open-function-explained/ |
10 |
| - with open("sowpods.txt", 'r') as f: |
11 |
| - words = f.readlines() |
| 5 | + # This function picks a random word from the SOWPODS dictionary. |
| 6 | + # open the sowpods dictionary as a text file in readable format |
| 7 | + # for more information on opening files visit https://pythontips.com/2014/01/15/the-open-function-explained/ |
| 8 | + |
| 9 | + with open("sowpods.txt", 'r') as f: |
| 10 | + words = f.readlines() |
12 | 11 |
|
13 |
| - # generate a random index |
14 |
| - # -1 because len(words) is not a valid index into the list `words` |
15 |
| - index = random.randint(0, len(words) - 1) |
| 12 | + # generate a random index |
| 13 | + # -1 because len(words) is not a valid index into the list `words` |
| 14 | + index = random.randint(0, len(words) - 1) |
16 | 15 |
|
17 |
| - # print out the word at that index |
18 |
| - # the .strip() function removes all trailing spaces before and after the word |
19 |
| - word = words[index].strip() |
20 |
| - return word |
| 16 | + # print out the word at that index |
| 17 | + # the .strip() function removes all trailing spaces before and after the word |
| 18 | + word = words[index].strip() |
| 19 | + return word |
21 | 20 |
|
22 | 21 |
|
23 | 22 | def ask_user_for_next_letter():
|
24 |
| - letter = input("Guess your letter: ") |
25 |
| - return letter.strip().upper() |
| 23 | + letter = input("Guess your letter: ") |
| 24 | + return letter.strip().upper() |
26 | 25 |
|
27 | 26 |
|
28 | 27 | def generate_word_string(word, letters_guessed):
|
29 |
| - output = [] |
30 |
| - for letter in word: |
31 |
| - if letter in letters_guessed: |
32 |
| - output.append(letter.upper()) |
33 |
| - else: |
34 |
| - output.append("_") |
35 |
| - |
36 |
| - # creates a string from the members of the list by using whitespace as a separator |
37 |
| - return " ".join(output) |
| 28 | + output = [] |
| 29 | + for letter in word: |
| 30 | + if letter in letters_guessed: |
| 31 | + output.append(letter.upper()) |
| 32 | + else: |
| 33 | + output.append("_") |
| 34 | + |
| 35 | + # creates a string from the members of the list by using whitespace as a separator |
| 36 | + return " ".join(output) |
| 37 | + |
38 | 38 |
|
39 | 39 | # make sure that the module is being run standalone and not imported by another user
|
40 | 40 | # visit http://ibiblio.org/g2swap/byteofpython/read/module-name.html for more information
|
41 | 41 | if __name__ == '__main__':
|
42 |
| - WORD = pick_random_word() |
43 |
| - |
44 |
| - # creates a set containing the letters of WORD |
45 |
| - letters_to_guess = set(WORD) |
46 |
| - |
47 |
| - # creates an empty set |
48 |
| - correct_letters_guessed = set() |
49 |
| - incorrect_letters_guessed = set() |
50 |
| - num_guesses = 0 |
51 |
| - |
52 |
| - print("Welcome to Hangman!") |
53 |
| - while (len(letters_to_guess) > 0) and num_guesses < 6: |
54 |
| - guess = ask_user_for_next_letter() |
55 |
| - |
56 |
| - # check if we already guessed that |
57 |
| - # letter |
58 |
| - if guess in correct_letters_guessed or \ |
59 |
| - guess in incorrect_letters_guessed: |
60 |
| - # print out a message |
61 |
| - print("You already guessed that letter.") |
62 |
| - continue |
63 |
| - |
64 |
| - # if the guess was correct |
65 |
| - if guess in letters_to_guess: |
66 |
| - # update the letters_to_guess |
67 |
| - letters_to_guess.remove(guess) |
68 |
| - # update the correct letters guessed |
69 |
| - correct_letters_guessed.add(guess) |
70 |
| - else: |
71 |
| - incorrect_letters_guessed.add(guess) |
72 |
| - # only update the number of guesses |
73 |
| - # if you guess incorrectly |
74 |
| - num_guesses += 1 |
75 |
| - |
76 |
| - word_string = generate_word_string(WORD, correct_letters_guessed) |
77 |
| - print(word_string) |
78 |
| - print("You have {} guesses left".format(6 - num_guesses)) |
79 |
| - |
80 |
| - # tell the user whether they have won or lost |
81 |
| - if num_guesses < 6: |
82 |
| - print("Congratulations! You correctly guessed the word {}".format(WORD)) |
83 |
| - else: |
84 |
| - print("Sorry, you lost! Your word was {}".format(WORD)) |
| 42 | + WORD = pick_random_word() |
| 43 | + |
| 44 | + # creates a set containing the letters of WORD |
| 45 | + letters_to_guess = set(WORD) |
| 46 | + |
| 47 | + # creates an empty set |
| 48 | + correct_letters_guessed = set() |
| 49 | + incorrect_letters_guessed = set() |
| 50 | + # since the classic order for hangman game takes 8 lost chances to hang the man |
| 51 | + num_guesses = 8 |
| 52 | + |
| 53 | + print("Welcome to Hangman!") |
| 54 | + while (len(letters_to_guess) > 0) and num_guesses > 0: |
| 55 | + guess = ask_user_for_next_letter() |
| 56 | + |
| 57 | + # check if we already guessed that |
| 58 | + # letter |
| 59 | + if guess in correct_letters_guessed or guess in incorrect_letters_guessed: |
| 60 | + # print out a message |
| 61 | + print("You already guessed that letter.") |
| 62 | + continue |
| 63 | + |
| 64 | + # if the guess was correct |
| 65 | + if guess in letters_to_guess: |
| 66 | + # update the letters_to_guess |
| 67 | + letters_to_guess.remove(guess) |
| 68 | + # update the correct letters guessed |
| 69 | + correct_letters_guessed.add(guess) |
| 70 | + else: |
| 71 | + incorrect_letters_guessed.add(guess) |
| 72 | + # only update the number of guesses |
| 73 | + # if you guess incorrectly |
| 74 | + num_guesses -= 1 |
| 75 | + |
| 76 | + word_string = generate_word_string(WORD, correct_letters_guessed) |
| 77 | + print(word_string) |
| 78 | + print("You have {} guesses left".format(num_guesses)) |
| 79 | + |
| 80 | + # tell the user whether they have won or lost |
| 81 | + if num_guesses > 0: |
| 82 | + print("Congratulations! You correctly guessed the word {}".format(WORD)) |
| 83 | + else: |
| 84 | + print("Sorry, you lost! Your word was {}".format(WORD)) |
0 commit comments