与另一个值相比更新值

时间:2016-09-28 20:51:12

标签: python string list variables indexing

我有这个代码是一个刽子手游戏。我使用while(there are blanks in the word):。然后我使用for循环来传输可用的列表版本,然后我使用它们可以看到的字符串版本。

  

我应该如何在不打印出大量_s的情况下使用不可见版本更新可见版本?

**Here is some code to refer to.**
```

# -*- coding: utf-8 -*-

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.
    """
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    return wordlist

wordlist = load_words()
word_to_guess = random.choice(wordlist)

# end of helper code.  A word to guess has been stored in the string
# variable 'word_to_guess'. +9

def hangman(word_to_guess):
    solvinglst = []
    solvingstr = " "
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    num_guesses = len(word_to_guess)
    for i in range(len(word_to_guess)):
        solvinglst += "_"
    print("Welcome to Hangman!")
    print("-------------------------------------------------------------")
    while("_" in solvinglst):            
        for i in solvinglst:
            if solvingstr != solvinglst:
                solvinglst[i].copy(solvingstr)
        print(solvingstr)
        print("You have ",num_guesses,"guesses.")
        letter = input("What letter do you want to guess?:")
        if letter in word_to_guess:
            for i in range(len(word_to_guess)):                    
                for x in range(len(alphabet)):
                        if alphabet[x] == letter:
                            alphabet = alphabet.replacea(alphabet[x],"_")
            if word_to_guess[i] == letter:
                solvinglst[i] = letter

        if letter not in word_to_guess:
            num_guesses -= 1
        if num_guesses < 1 :
            print("You lose.")
            solvinglst = word_to_guess
        if "_" not in solvinglst:
            print(solvinglst)
            break
        print(num_guesses)
        print(alphabet)

hangman(word_to_guess)

我上面的代码返回一个随机单词供他们猜测。 正如您所看到的,我已经知道该怎么做,它不起作用,但我需要一个简单的解决方案。

0 个答案:

没有答案