我正在處理的一個version子手的Python版本,工作的.txt文件的單詞列表。出於某種原因,在初次玩遊戲後的劇本中,腳本不會顯示某些字母。其他一切都按我想要的方式工作,或多或少。Python的hang子手不透露字母
我的代碼:
import random
wordlist = open("wordlist.txt").read().split()
word = random.choice(wordlist)
strikes = 0
hidden_letters = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
guesses = []
def new_word():
global word
global guesses
word = random.choice(wordlist)
for letter in set(word):
if letter in alphabet:
hidden_letters.append(letter)
guesses = []
def current_progress():
for letter in word:
if letter in hidden_letters:
print '_',
elif letter == ' ':
print ' ',
else:
print letter,
print "\n"
def play_again():
global strikes
print "Would you like to play again?"
answer = raw_input("y/n: ")
if answer == "y":
strikes = 0
main()
elif answer == "n": exit(0)
else:
print "That's not a valid answer."
play_again()
def letter_in_word(x):
global strikes
global hidden_letters
if x in word:
hidden_letters.remove(x)
print "That letter is in the word."
current_progress()
if hidden_letters == []:
print "You win!"
play_again()
else:
print "You have %d strike(s)." % strikes
elif not x in word:
print "That letter is not in the word."
current_progress()
strikes = strikes + 1
print "You have %d strike(s)." % strikes
def main():
new_word()
current_progress()
global strikes
while strikes < 6 and not hidden_letters == []:
print "Guess a letter. \n Letters that have been already guessed are:", guesses
guess = raw_input("> ")
if guess in alphabet and len(guess) == 1:
if not guess in guesses:
guesses.append(guess)
letter_in_word(guess)
else:
print "You've already guessed that letter. Pick another."
current_progress()
print "You have %d strikes." % strikes
else:
print "Sorry, that's not a valid guess."
current_progress()
print "You have %d strikes." % strikes
if strikes == 6:
print "Oop! You lose."
print "The answer was:", word
play_again()
print "Welcome to Hangman!"
print "Six strikes and you lose."
print "----------"
main()
你能給我們提一個你遇到的問題的具體例子嗎?或者嘗試使用調試器來縮小問題的範圍? – prelic 2012-03-24 20:01:12