【问题标题】:String object does not support item assignment字符串对象不支持项赋值
【发布时间】:2019-01-13 23:03:38
【问题描述】:

作为一名初级程序员,我目前正在从事一个刽子手项目,其中给定的字母每次都必须返回。因此,我想使用一个列表,该列表由玩家给定的字母返回。但是,这给了我以下问题:

  File "Hangman.py", line 65, in <module>
    word_blank[i] = guess
TypeError: 'str' object does not support item assignment

这是我的代码的以下部分:

while game_on == 'yes':
    answer = select_word(selection)
    if answer != 'error123':
        word_blank = answer_form(answer)
        break

# start guessing letters
count = 1
guesses = []

while count <= 10:
    print(f'\n\nYour guess count:\t{count}')
    print(f'Your guesses:\t\t{guesses}')
    guess = input('Guess a letter: ').lower()
    count += 1
    guesses.append(guess)

    for i in range(0, len(answer)):
        if answer[i] == guess:
            word_blank[i] = guess
            break

    print(' '.join(word_blank))

print('\n\nGame over..')

答案形式为:

def answer_form(answer):
    word_blank = []
    word_blank.extend(answer)

    for i in range(0, len(word_blank)):
        word_blank[i] = '_'

    return(' '.join(word_blank))

我已经在 Stack Overflow 上检查了各种答案,但不幸的是,我无法将这些与我的个人代码很好地联系起来。我觉得this link 中的解决方案很接近,但不幸的是它一直没有结果。我应该如何改进我的代码以消除错误,同时能够留在循环内将所有猜测的字母添加到word_blank[i]

【问题讨论】:

  • 在哪里初始化 word_blank?您需要显示所有相关代码。
  • @CupinaCoffee 我添加了 word_blank 初始化的部分以及它是如何构造的。
  • 字符串是不可变的——意思是:你不能改变它们。您可以创建一个新的 f.e.通过切片:s = "abcdk" 然后s = s[:-1]+""efghij" + s[-1]。对于您的游戏,使用角色列表,您可以更轻松地操作它们。

标签: python string variable-assignment


【解决方案1】:

您的 def answer_form(answer): 返回一个连接列表 - 使其成为一个字符串。您不能修改字符串,它们是不可变的(参见 f.e. Immutable vs Mutable types)。但是,您可以重新创建它们,例如通过切片或将它们用作字符列表。请参阅Change one character in a string?


如果你使用sets,你可以简化你的刽子手。对于尚未猜到的任何字符,打印'_' 更容易,然后操作字符串(并且您也避免操作列表;o):

data = ["hang","man","carrot","supercalifragilisticexpialidocious"]

# random.choice solver (I am lazy ;o) ) - set to False for human player
autosolve = True

import random
from string import ascii_lowercase 
aslow = set(ascii_lowercase) 

# what should the user guess? random choice from data
what = random.choice(data).lower()

# store the length and characters used in what instead of calculating each time
lwhat = len(what)
swhat = set(what)
# these letters where already guessed 
guessed = set()

# amount of tries reached, we quit when twice the amount of characters got tried
tries = 0
while True and tries < 2*lwhat:

    # use list comprehension to print _ if a char was not guessed yet, else print char
    print("The secret word is: ", ' '.join(x if x in guessed else "_" for x in what))

    # we are done if all characters of what are guessed 
    if swhat.issubset(guessed):
        print(f"You did it! It took you {tries} tries") 
        break

    if not autosolve: 
        guess = input("Guess one letter or the complete word:").lower() 
    else:
        guess = random.choice(list(aslow))
        print("Guess one letter or the complete word:", guess)
        aslow.remove(guess)

    lguess = len(guess)

    # only count 1-character and full word guesses
    if lguess not in (1,lwhat):
        print("1 letter or the while word, try again") 
        continue

    tries += 1
    # one character was guessed
    if lguess == 1:
        guessed.add(guess) # add to set and print some feedback
        if guess in what:  
            print("Correct.")
        else:
            print("beeeeeep - that was wrong.")

    # whole word was guessed
    elif guess == what:
        print(f"You did it! It took you {tries} tries") 
        break
else:
    print("Too many tries: try again later")

print("The end")

获胜条件是:你在所有猜测的字符中寻找is a subset的单词集合。

输出:

The secret word is:  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Guess one letter or the complete word: s
Correct.
The secret word is:  s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: n
beeeeeep - that was wrong.
The secret word is:  s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: b
beeeeeep - that was wrong.
The secret word is:  s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: v
beeeeeep - that was wrong.
The secret word is:  s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: j
beeeeeep - that was wrong.
The secret word is:  s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: e
Correct.
The secret word is:  s _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: q
beeeeeep - that was wrong.
The secret word is:  s _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: g
Correct.
The secret word is:  s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e _ _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: x
Correct.
The secret word is:  s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: w
beeeeeep - that was wrong.
The secret word is:  s _ _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ _ s
Guess one letter or the complete word: u
Correct.
The secret word is:  s u _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ _ _ _ _ u s
Guess one letter or the complete word: o
Correct.
The secret word is:  s u _ e _ _ _ _ _ _ _ _ g _ _ _ s _ _ _ e x _ _ _ _ _ _ o _ _ o u s
Guess one letter or the complete word: t
Correct.
The secret word is:  s u _ e _ _ _ _ _ _ _ _ g _ _ _ s t _ _ e x _ _ _ _ _ _ o _ _ o u s
Guess one letter or the complete word: c
Correct.
The secret word is:  s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: m
beeeeeep - that was wrong.
The secret word is:  s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: h
beeeeeep - that was wrong.
The secret word is:  s u _ e _ c _ _ _ _ _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: f
Correct.
The secret word is:  s u _ e _ c _ _ _ f _ _ g _ _ _ s t _ c e x _ _ _ _ _ _ o c _ o u s
Guess one letter or the complete word: i
Correct.
The secret word is:  s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i _ o c i o u s
Guess one letter or the complete word: k
beeeeeep - that was wrong.
The secret word is:  s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i _ o c i o u s
Guess one letter or the complete word: d
Correct.
The secret word is:  s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i d o c i o u s
Guess one letter or the complete word: y
beeeeeep - that was wrong.
The secret word is:  s u _ e _ c _ _ i f _ _ g i _ i s t i c e x _ i _ _ i d o c i o u s
Guess one letter or the complete word: a
Correct.
The secret word is:  s u _ e _ c a _ i f _ a g i _ i s t i c e x _ i a _ i d o c i o u s
Guess one letter or the complete word: r
Correct.
The secret word is:  s u _ e r c a _ i f r a g i _ i s t i c e x _ i a _ i d o c i o u s
Guess one letter or the complete word: p
Correct.
The secret word is:  s u p e r c a _ i f r a g i _ i s t i c e x p i a _ i d o c i o u s
Guess one letter or the complete word: l
Correct.
The secret word is:  s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s
You did it! It took you 25 tries
The end

不过,“AI”对于简短的词来说真的很糟糕:

The secret word is:  _ _ _ _ _ _
Guess one letter or the complete word: q
beeeeeep - that was wrong.
The secret word is:  _ _ _ _ _ _
Guess one letter or the complete word: v
beeeeeep - that was wrong.
The secret word is:  _ _ _ _ _ _
Guess one letter or the complete word: p
beeeeeep - that was wrong.
The secret word is:  _ _ _ _ _ _
Guess one letter or the complete word: o
Correct.
The secret word is:  _ _ _ _ o _
Guess one letter or the complete word: y
beeeeeep - that was wrong.
The secret word is:  _ _ _ _ o _
Guess one letter or the complete word: a
Correct.
The secret word is:  _ a _ _ o _
Guess one letter or the complete word: d
beeeeeep - that was wrong.
The secret word is:  _ a _ _ o _
Guess one letter or the complete word: w
beeeeeep - that was wrong.
The secret word is:  _ a _ _ o _
Guess one letter or the complete word: m
beeeeeep - that was wrong.
The secret word is:  _ a _ _ o _
Guess one letter or the complete word: i
beeeeeep - that was wrong.
The secret word is:  _ a _ _ o _
Guess one letter or the complete word: t
Correct.
The secret word is:  _ a _ _ o t
Guess one letter or the complete word: j
beeeeeep - that was wrong.
Too many tries: try again later
The end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-07-24
    • 2016-10-24
    • 2023-03-30
    相关资源
    最近更新 更多