Python刽子手,取代字母?

时间:2014-11-14 19:08:19

标签: python

如果猜到了,我如何让我的python hangman游戏用正确的字母替换变量blanks中的下划线。

我有正确的if语句,但我不知道如何取代这些字母。

这就是我的尝试:

def main()
    if choice in correctAnswer:
        print("\nCorrect!\n------...\n")

        index = 0
        while index < len(correctAnswer):
            index = correctAnswer.find(choice, index)
            if index == -1:
                break
            print(index)
            index += 1

        used.append(choice)

        places = []
        place=correctAnswer.index(choice)
        blanks[place]=choice
        blanks=''.join(blanks)
main()

这给了我typeError:TypeError: 'str' object does not support item assignment

关于如何做到这一点的任何想法?

由于

更新:

blanks = list(makeBoard(correctAnswer))

print (correctAnswer)

def main():
    used = []
    print(blanks)
    choice = input("\nEnter a letter:")

    if len(choice) == 1 and special_match(choice) == True:

        if choice in correctAnswer:
            print("\nCorrect!\n--------------------------------------------------------------------\n")

            index = 0
            while index < len(correctAnswer):
                index = correctAnswer.find(choice, index)
                if index == -1:
                    break
                print(index)
                index += 1

            used.append(choice)

            [choice if letter == choice else blank for blank, letter in zip(blanks, correctAnswer)]

            main()

3 个答案:

答案 0 :(得分:5)

使用列表会更好,因为字符串是不可变的

>>> blanks = list('_______')
>>> answer = 'hangman'
>>> guess = 'g'

>>> blanks[answer.index(guess)] = guess
>>> blanks
['_', '_', '_', 'g', '_', '_', '_']

因此,这会使blanks变为可变,因此您可以随意替换元素。然后,当您想以字符串

将其显示给用户时
>>> ''.join(blanks)
'___g___'

列表理解可以处理重复的字母

>>> [guess if letter == guess else blank for blank, letter in zip(blanks, answer)]
['_', 'a', '_', '_', '_', 'a', '_']

修改
请参阅以下快速演示,请注意您应该添加错误猜测的处理

answer = 'hangman'
blanks = list('_'*len(answer))
for guess in set('hangman'):
    blanks = [guess if letter == guess else blank for blank, letter in zip(blanks, answer)]
    print(''.join(blanks))

输出

h______
h__g___
h__gm__
ha_gma_
hangman

答案 1 :(得分:0)

这就是我所做的:

if guess_input in current_word:
    cpos = 0
    for letter in current_word:
        if letter == guess_input:
            guess = guess[:cpos] + guess[cpos].replace("*",guess_input) + guess[cpos + 1:]
        cpos += 1

猜测等于“ *” * len(current_word)

在您的情况下,您将使用“ _”。

这适用于重复的字母。

答案 2 :(得分:0)

我最近将此作为爱好项目进行了尝试。

我用正则表达式来完成这项工作。

inputRegex = re.compile(r'^\w{1}$')  #regex for checking if only one alphabet has been input

thisGuess = self.myGuess.text().upper()
check = re.findall(thisGuess, self.qDisp)
# now if there is match will be non zero
quest = list(self.qDisp) # conv the blank word display into list for easy replace
#find the position of the guess in the string
for check in re.finditer(thisGuess, self.quest):
   i = check.start() #this gives the starting position of the match
   quest[i] = thisGuess  # 


self.qDisp = "".join(quest) #to join the list into string from list created above loop