gpt4 book ai didi

python - 出现类型错误,但我找不到原因

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:21 25 4
gpt4 key购买 nike

这段代码以前可以工作,但现在给我带来了问题。

def stringsAreFun():
string1 = input("Enter a String you want to modify: ") #stores the user imput to string1 variable
stringLength = len(string1) #stores stringlen to a variable for future, repitive calls
print("The length is ", stringLength)
print("The first character is " + string1[0]
+ " and the last character is " + string1[stringLength - 1])
listString = list[string1]
if stringLength >= 6:
#creates a list of characters from string1 because strings are immuntable in python
#http://www.tutorialspoint.com/python/python_lists.htm
listString[stringLength - 1] = 'C'
listString[stringLength - 2] = 'B'
listString[stringLength - 3] = 'A'
print(''.join(listString)) #joins the list(x) into a string to be printed
#http://www.tutorialspoint.com/python/string_join.htm
if stringLength >= 6:
listString[0] = 'X'
listString[1] = 'Y'
listString[2] = 'Z'
print(''.join(listString))

if stringLength >= 2 & stringLength <= 5:
listString[0] = '1'
print(''.join(listString))
if stringLength >= 2 & stringLength <= 5:
listString[stringLength - 1] = '0'
print(''.join(listString))

if stringLength % 2 == 0:
print("The length of the string is an even number!")

else:
print("The length of the string is an odd number")

我得到这个输出:

TypeError: 'type' object is not subscriptable

最佳答案

这一行:

listString = list[string1]

没有做你想做的事。

listString = list(string1)

关于python - 出现类型错误,但我找不到原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com