Python noob hangman问题

时间:2014-05-30 00:33:46

标签: python

我正在尝试学习Python,我正在编写一个刽子手游戏(这似乎是Python的“hello world”)。我想我已经把它搞砸了,但是我写的用正确的猜测字母替换隐藏字母的功能是行不通的。对于我的生活,它似乎应该做我想要的,但它将用正确猜到的一个字母替换整个隐藏的单词。

以下是具体功能:

def update_word_guessed(guess):
    global word_guessed
    for i in xrange(word.count(guess)):
        for x in xrange(len(word)):
            if guess == word[x]:
                word_guessed = word_guessed.replace(word_guessed[x], word[x])
                print word_guessed, 'wg[x] =', word_guessed[x], 'word[x] = ', word[x]
    return word_guessed

以下是整个事情:

import random
import string

WORDLIST_FILENAME = "words.txt"

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = string.split(line)
    print "  ", len(wordlist), "words loaded."
    return wordlist

def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code
# -----------------------------------

# actually load the dictionary of words and point to it with 
# the wordlist variable so that it can be accessed from anywhere
# in the program
wordlist = load_words()
print '\n'
word = choose_word(wordlist)
word_guessed = []
for i in xrange(len(word)):
    word_guessed.append('-')
word_guessed = ''.join(word_guessed)
guesses = 10
correct_guesses = 0
letters_left = 'abcdefghijklmnopqrstuvwxyz'



def initiate_game():
    """ Starts game """
    print 'Welcome to the game Hangman!'
    print 'I am thinking of a word ', len(word), 'letters long'
    print '---------------'
    while guesses > 0 and correct_guesses < len(word):
        print 'You have ', guesses, 'guesses left.'
        print 'Unused letters: ', letters_left
        guess = str.lower(raw_input('Guess a letter: '))
        print word_guessed
        if check_letter_available(guess) == False:
            print 'Sorry, it looks like you have already used that letter.'
            continue
        check_guess(guess)
    if correct_guesses == len(word):
        print 'Congradulations, you win! '
        print word_guessed
    else:
        print 'You are out of guesses, the word was: ', word

def remove_guessed_letter(guess):
    global letters_left
    letters_left = letters_left.replace(guess, '~')
    return letters_left

def check_letter_available(guess):
    if letters_left.find(guess) == -1:
        return False
    else:
        return True

def update_word_guessed(guess):
    global word_guessed
    for i in xrange(word.count(guess)):
        for x in xrange(len(word)):
            if guess == word[x]:
                word_guessed = word_guessed.replace(word_guessed[x], word[x])
                print word_guessed, 'wg[x] =', word_guessed[x], 'word[x] = ', word[x]
    return word_guessed

#def update_word_guessed(guess):


def check_guess(guess):
    global guesses
    global correct_guesses
    guesses -= 1
    if word.find(guess) == -1:
        print 'Sorry, ', guess, 'is not a part of my word: ', word_guessed
        remove_guessed_letter(guess)
        return False
    else:
        correct_guesses += 1
        print 'Good job!'
        remove_guessed_letter(guess)
        update_word_guessed(guess)
        print word_guessed
        return True

##    for i in xrange(len(word)):
##        if guess == word[i]:
##            correct_guesses += 1
##            print 'Good job!'
##            remove_guessed_letter(guess)
##            update_word_guessed(guess)
##            print ' '.join(word_guessed)
##            return True
##        else:
##            print 'Sorry, ', guess, 'is not a part of my word: '.join(word_guessed)
##            remove_guessed_letter(guess)
##            return False


initiate_game()

1 个答案:

答案 0 :(得分:3)

你用这里的字母替换了单词中的所有破折号:

word_guessed = word_guessed.replace(word_guessed[x], word[x])

str.replace查找第一个参数的所有实例,并用第二个参数替换它们。你只想改变一个字母:

word_guessed = word_guessed[:x] + word[x] + word_guessed[x+1:]

可能还有其他问题,但这就是您所描述的问题。享受!