此代碼以前工作,但現在給我的問題。獲取類型錯誤,我找不到原因
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")
我m如果此輸出:
TypeError: 'type' object is not subscriptable
將來,添加有關發生異常的行的信息。這意味着更少的猜測和更快的答案。 – viraptor