这段代码以前可以运行,但是现在却给我带来了问题。
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/