我:与子分配蟒蛇错误
LETTERS = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
results = ['BAN', 'ANA', 'PEE', 'L']
length = len(LETTERS)
for segment in results:
# get the numerical position of the character
first_char = LETTERS.index(segment[0])
# get the shift number
first_char = (first_char + 0) % length
#first character shift
segment[0] = LETTERS[first_char]
我如下得到一个错误:
segment[0] = LETTERS[first_char]
TypeError: 'str' object does not support item assignment
如果我修改这个程序,使得它不是一个for循环它的工作原理,但在循环是我得到消息的地方。这是为什么?
字符串是不可改变的。你不能改变它中的一个角色。 – Santa