2012-03-24 10 views
2

私は、.txtファイルのワードリストから動作するhangmanのPython版で作業しています。何らかの理由で、最初のプレイスルー後のプレイスルーでは、スクリプトは特定の文字を表示しません。他のすべては私がそれがほしいと思うように働く。python hangman lettersを公開していません

マイコード:

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() 
+1

問題の具体例を教えていただけますか?または、問題を絞り込むためにデバッガを試してみてください。 – prelic

答えて

2

あなたの問題:プレイヤーは現在の単語にし、前の単語に存在する文字を推測しますが、前の単語に推測されなかったとき(プレイヤーがそのゲームを失いました) remove(x)はその文字の最初のインスタンスのみを削除するため、文字はhidden_lettersのリストに残ります。言い換えれば、あなたのリストには、あなたのコードの暗黙の要求に違反する、いくつかの実行中に同じ文字の2つが含まれています。新しい単語の文字を追加する前に、new_word()関数にhidden_letters = []を追加して修正することができます。 Python tutorial(強調鉱山)から

list.remove(x)

その値はXされるリストから最初のアイテムを削除します。そのような項目がない場合はエラー です。

+0

そして、重複の可能性を回避するセットを使用することで、全体を完全になくすことができます。 –

+0

ありがとう、これは私の問題をすばらしく解決しました。 – thepotato329

2

はまた、私はあなたがnew_word()のbegginingに声明

del hidden_letters[:] 

を追加することができると思います。 hidden_lettersのリストはクリアされます。

関連する問題