Опубликован: 12.07.2013 | Доступ: свободный | Студентов: 733 / 20 | Длительность: 37:41:00
Лекция 9:

Hangman

Аннотация: In this chapter, we will actually write out the code for Hangman.
Ключевые слова: this, new, experiment, with, programming, interactive, shell, First, Data, AS, AND, VALUES, CAN, data type, dictionary, chapter, FROM, simple variable, Write, string, ONE, Line, if, quote, begin, end, escape character, hard, read, LIKE, example, NOT, indentation, block, python, WHERE, ALL, programming conventions, constant variable, change, programmer, special, meaning, reason, Copy, TIME, vary, typing, set, setting, integer, define, constant, even, used, help, long, list, try, store, individualization, square bracket, GET, item, SEE, evaluate, INDEX, second, very, group, expression, string concatenation, enter, accessing, operator, joining, return, boolean, wasp, CHECK, EXISTS, remove, statement, del, short, DELETE, function, let, LAWN, finally, point, RED, ITS, method, call, pass, argument, attach, method call, period, uppercase, final value, evaluation, important, look, Reverse, MOST, Add, cause, error, object-oriented programming, BIT, advanced, book, assignment statement, string data type, long string, Full, word, list item, split, space, comma, result, original, secret, computer, incorrect, guess, ascii art, Object, generate, huge, fit, screen, save, useful, looping, CONDITION, true, keyword, SEQUENCE, colon, execution, NEXT, indent, CURSOR, waiting, blank line, execute, convert, output, Character, iteration, newline character, underscore, equal, USER, CAT, else, mistake, leave, off, lowercase letter, parentheses, parameter passing, part, back, flow, Chart, function definition, appropriate, situational, survivability, arm, coincidence, program termination, realm, sophisticated, sketch, find, lines of code, improve, player, Left, lost, modification, separate, color, Shape, collection, curly brace, keyboard, square, braces, right, file type, ordered list, interpreter, random, PARAMETER, choose, MODULE, exact, sign, assign, mean, complex, IDEA, paper, ITERATE, create, sequential, previous, EXCEPT

Topics Covered In This Chapter:

  • Methods
  • The append() list method
  • The lower() and upper() string methods
  • The reverse() list method
  • The split() string method
  • The range() function
  • The list() function
  • for loops
  • elif statements
  • The startswith() and endswith() string methods.
  • The dictionary data type.
  • key-value pairs
  • The keys() and values() dictionary methods
  • Multiple variable assignment, such as a, b, c = [1, 2, 3]

This game introduces many new concepts. But don't worry; we'll experiment with these programming concepts in the interactive shell first. Some data types such as strings and lists have functions that are associated with their values called methods. We will learn several different methods that can manipulate strings and lists for us. We will also learn about a new type of loop called a for loop and a new type of data type called a dictionary. Once you understand these concepts, it will be much easier to understand the game in this chapter: Hangman.

You can learn more from Wikipedia: http://en.wikipedia.org/wiki/Hangman_(game)

Hangman's Source Code

This chapter's game is a bit longer than our previous games. You can either type in the code below directly into the file editor (which I recommend) or you can obtain the code from this book's website. To grab the code from the web, in a web browser go to the URL http://inventwithpython.com/chapter9 and follow the instructions for downloading the source code.

hangman.py

This code can be downloaded from http://inventwithpyt.hon.com/hangman.py

If you get errors after typing this code in, compare it to the book's code with the online

diff tool at http://inventwithpython.com/diff or email the author at

al@inventwithpython.com

1.  import random
2.  HANGMANPICS = [ ' ' '
3.
4.    +---+                                         
5.    |   |                                         
6.        |                                         
7.        |                                         
8.        |                                         
9.        |                                         
10. =========''', ''' 
11.
12.   +---+                                         
13.   |   |                                         
14.   O   |                                         
15.       |                                         
16.       |                                         
17.       |                                         
18. =========''', ''' 
19.
20.   +---+                                         
21.   |   |                                         
22.   O   |                                         
23.   |   |                                         
24.       |                                         
25.       |                                         
26. =========''', ''' 
27.
28.   +---+
29.   |   | 
30.   O   | 
31.  /|   |
32.       |
33.       |
34. =========''', '''
35.
36.   +---+
37.   |   |
38.   O   |
39.  /|\  |
40.       |
41.       |
42. =========''', '''
43.
44.   +---+
45.   |   |
46.   O   |
47.  /|\  |
48.  /    |
49.       |
50. =========''', '''
51.
52.   +---+
53.   |   |
54.   O   |
55.  /|\  |
56.  / \  |
57.       |
58. =========''']
59. words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer 
    dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey 
    moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino 
    salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey 
    turtle weasel whale wolf wombat zebra'.split()
60.
61. def getRandomWord(wordList):
62.     # This function returns a random string from the passed list of strings.
63.     wordIndex = random.randint(0, len(wordList) - 1)
64.     return wordList[wordIndex] 
65.
66. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
67.     print(HANGMANPICS[len(missedLetters)])
68.     print() 
69.
70.     print('Missed letters:', end=' ')
71.     for letter in missedLetters:
72.         print(letter, end=' ')
73.     print() 
74.
75.     blanks = '_' * len(secretWord) 
76.
77.     for i in range(len(secretWord)): # replace blanks with correctly guessed letters
78.         if secretWord[i] in correctLetters:
79.             blanks = blanks[:i] + secretWord[i] + blanks [i+1:]
80.
81.     for letter in blanks: # show the secret word with spaces in between each letter
82.                      print(letter, end=' ')
83.            print() 84.
85. def getGuess(alreadyGuessed):
86.     # Returns the letter the player entered. This function makes sure the player 
     entered a single letter, and not something else.
87.     while True:
88.         print('Guess a letter.')
89.         guess = input()
90.         guess = guess.lower()
91.         if len(guess) != 1:
92.             print('Please enter a single letter.')
93.         elif guess in alreadyGuessed:
94.             print('You have already guessed that letter. Choose again.')
95.         elif guess not in 'abcdefghijklmnopqrstuvwxyz':
96.             print('Please enter a LETTER.')
97.         else:
98.             return guess 99.
100. def playAgain():
101.     # This function returns True if the player wants to play again, otherwise it returns False.
102.     print('Do you want to play again? (yes or no)')
103.     return input().lower().startswith('y') 
104.
105.
106. print('H A N G M A N')
107. missedLetters = ''
108. correctLetters = ''
109. secretWord = getRandomWord(words)
110. gameIsDone = False 111.
112. while True:
113.     displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
114.
115.     # Let the player type in a letter.
116.     guess = getGuess(missedLetters + correctLetters)
117.
118.     if guess in secretWord:
119.         correctLetters = correctLetters + guess 
120.
121.         # Check if the player has won
122.         foundAllLetters = True
123.         for i in range(len(secretWord)):
124.             if secretWord[i] not in correctLetters:
125.                 foundAllLetters = False
126.                 break
127.         if foundAllLetters:
128.             print('Yes! The secret word is "' + secretWord + '"! You have won!')
129.             gameIsDone = True
130.    else:
131.        missedLetters = missedLetters + guess
132.
133.         # Check if player has guessed too many times and lost
134.          if len(missedLetters) == len(HANGMANPICS) - 1:
135.             displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
136.             print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + 
     str(len (correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
137.             gameIsDone = True 
138 .
139.     # Ask the player if they want to play again (but only if the game is done).
140.      if gameIsDone:
141.          if playAgain() :
142.             missedLetters = ''
143.             correctLetters = ''
144.             gameIsDone = False
145.              secretWord = getRandomWord(words)
146.        else:
147.                             break

How the Code Works

1. import random

The Hangman program is going to randomly select a secret word from a list of secret words. This means we will need the random module imported.

2.  HANGMANPICS = [ ' ' '
3.
4.    +---+                                         
5.    |   |                                         
6.        |                                         
7.        |                                         
8.        |                                         
9.        |                                         
10. =========''', ''' 

...the rest of the code is too big to show here...

This "line" of code a simple variable assignment, but it actually stretches over several real lines in the source code. The actual "line" doesn't end until line 58. To help you understand what this code means, you should learn about multi-line strings and lists:

Patryk Jasiński
Patryk Jasiński
Польша
Юрий Гоголев
Юрий Гоголев
Россия, Тула, ТулГУ, 2002