Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 处理列表中多次出现的图元_Python - Fatal编程技术网

Python 处理列表中多次出现的图元

Python 处理列表中多次出现的图元,python,Python,说到编码,我还是一个新手,我正在尝试创建我自己的刽子手游戏。当我猜一个单词中出现不止一次的字符时,我遇到了一些困难 以下是我的代码片段: def random_word(): #word generator randomized = random.randint(0,(len(content_words)-1)) word_to_guess = content_words[randomized].lower() splitted = [] word_progress = [] for chara

说到编码,我还是一个新手,我正在尝试创建我自己的刽子手游戏。当我猜一个单词中出现不止一次的字符时,我遇到了一些困难

以下是我的代码片段:

def random_word():
#word generator
randomized = random.randint(0,(len(content_words)-1))
word_to_guess = content_words[randomized].lower()
splitted = []
word_progress = []
for character in word_to_guess:
    splitted.append(character.lower())
    word_progress.append("?")
counter = 0
while counter <= 5:
    print(word_to_guess)
    print(splitted)
    print(word_progress)
    #Start of the game
    options = str(input("Do you want to guess the word or the characters?: ").lower())
    #word
    if options == "word":
        guess_word = input("Please your guess of the word: ").lower()
        if guess_word == word_to_guess:
            print("Correct! The word was " + word_to_guess + " you only needed " + str(counter) + " tries!")
            break
        elif guess_word != word_to_guess:
            counter += 3
            print("You have entered the wrong word ! You now have " + str(5-counter) + " tries left!")
            continue
            #characters
    elif options == "characters":
        guess_character = input("Please enter the character you would like to enter!: ")
        if guess_character in splitted and len(guess_character) ==  1:
            print("Correct! The character " + guess_character.upper() + " is in the word were looking for!" )
            for char in word_to_guess:
                if char == guess_character:
                    word_progress[word_to_guess.index(char)] = word_to_guess[word_to_guess.index(char)]
            continue
def random_word():
#字生成器
随机化=random.randint(0,(len(内容词)-1))
单词到猜测=内容单词[随机化].lower()
拆分=[]
word_进度=[]
对于word_to_guess中的字符:
拆分的.append(character.lower())
word\u进度。追加(“?”)
计数器=0

而计数器
index
将只返回字符第一次出现时的索引

您应该使用来同时迭代字符和索引:

for index, char in enumerate(word_to_guess):
    if char == guess_character:
        word_progress[index] = guess_character

index
将仅返回角色第一次出现时的索引

您应该使用来同时迭代字符和索引:

for index, char in enumerate(word_to_guess):
    if char == guess_character:
        word_progress[index] = guess_character