无法替换列表中的项目-“功能对象不支持项目分配”

问题描述 投票:0回答:1

我正在尝试用另一个字符替换列表中的某些字符。

import random
import string
import re

def main():
    wordInput()
    computerChoice(wordInput)




def wordInput():
    while(True): #while function to validate user input
        userWord = input("Please choose a word between 5 and 10 letters long: ")
        if len(userWord)<5 or len(userWord)>10:
            print("Error: Word is not between 5 and 10 letters!")
        else:
            break


    #stage 2: printing lines in place of characters to hide the word chosen
    print(" ")
    userWordHidden = list(userWord)
    for i in range(len(userWord)):
        userWordHidden[i] = "-"

    print("Your Word:", end =" ")
    for i in range(len(userWord)):
        print(userWordHidden[i], end =" ")

    print("\n")

    return userWordHidden


def computerChoice(userWordHidden):
    compGuessCount = 0
    compChoice = random.choice(string.ascii_uppercase)

    while(True):
        compGuess = input('Does your word contain the letter %s? \
Please enter "Yes" or "No": ' % (compChoice))
        if compGuess == "Yes" or compGuess == "No":
            break
        else:
            print('Error: Please enter "Yes" or "No"')

    if compGuess == "Yes":
        letterLocation(userWordHidden, compChoice)

def letterLocation(userWordHidden, compChoice):
    userLetterLocation = input('What is the index position of the letter()s \
in the word?: ')
    letterIndex = re.findall("\d", userLetterLocation)

    userWordHidden[1] = "s"

    print(userWordHidden)

这是我正在研究的内容的一部分,但是如果您忽略其中的大部分内容,而只关注函子Location,最后您会看到我正在尝试测试用“ s”替换userWordHidden索引2,但是我收到以下错误:TypeError:“函数”对象不支持项目分配

有人知道为什么吗?我知道您无法更新字符串,但是可以使用列表,但是它不起作用?

python list append
1个回答
0
投票
def main():
    wordInput()
    computerChoice(wordInput)

您将函数wordInput传递给了computerChoice函数。我认为您需要的是什么

target_word = wordInput()
computerChoice(target_word)
© www.soinside.com 2019 - 2024. All rights reserved.