将一个值与另一个值进行比较

2024-10-02 04:17:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

How am I supposed to update the visible version with the non visible version without printing out a ridiculous amount of _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)

刽子手(猜猜看)

我有上面的代码返回一个随机词让他们猜。 正如你所看到的,我已经有了一个想法要做什么,它不工作,但我需要一个简单的解决方案,这段代码。你知道吗


Tags: ofthetoinforifnumword

热门问题