Я пытаюсь следовать учебному пособию, чтобы написать простую «игру в стиле палача». Она работает нормально, однако, если вы правильно угадываете букву, при первом неверном угадывании программа отображает 2-й график ASCII, а не 1-й.когда вы угадываете ваше второе неправильное письмо, появляется третье графическое изображение, но оно «искажается» следующим образом:
Извините, z не то, что мы ищем.
+---------+
| |
| O
| -|-
| / |
================
наконецВы делаете 3-е неверное предположение, и я получаю ошибку:
Traceback (most recent call last):
File "./HangMan.py", line 118, in <module>
start()
File "./HangMan.py", line 44, in start
while game():
File "./HangMan.py", line 79, in game
hangedman(letters_wrong)
File "./HangMan.py", line 39, in hangedman
print graphic[hangman]
IndexError: list index out of range
Вот мой код:
from random import *
player_score = 0
computer_score = 0
def hangedman(hangman):
graphic = [
"""
+---------+
|
|
|
|
|
================
""",
"""
+---------+
| |
| O
|
|
|
================
""",
"""
+---------+
| |
| O
| -|-
| / \
|
================
"""]
print graphic[hangman]
return
def start():
print "Lets play a game of Hangman"
while game():
pass
scores()
def game():
dictionary = ["box","ted","sad","bad"]
word = choice(dictionary)
word_length = len(word)
clue = word_length * ["_"]
tries = 3
letters_tried = ""
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score
while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) !=-1:
print "you've already picked", letter
else:
letters_tried = letters_tried = letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong +=1
print "Sorry,",letter,"isn't what we're looking for."
else:
print "Congratulations,",letter,"is correct."
for i in range(word_length):
if letter == word[i]:
clue[i] = letter
else:
print "Choose another"
hangedman(letters_wrong)
print " ".join(clue)
print "Guesses: ", letters_tried
if letters_wrong == tries:
print "Game Over"
print "The word was",word
computer_score += 1
break
if "".join(clue) == word:
print "you win!"
print "the word was", word
player_score += 1
break
return play_again()
def guess_letter():
print
letter = raw_input("Take a Guess at our mystery word:")
letter.strip()
letter.lower()
print
return letter
def play_again():
answer = raw_input("Would you like to play again? y/n: ")
if answer in ("y"):
return answer
else:
print "Thank you see you next time"
def scores():
global player_score, computer_score
print "Hight Scores"
print "Player: ", player_score
print "Computer: ", computer_score
if __name__ == '__main__':
start()