我的代碼假設將一個字符串轉換爲一個函數,然後將其中每個字符的大寫字母從h切換到H和E,但是我在某種程度上遇到了錯誤 爲什麼?爲什麼我在python上得到這個錯誤,我該如何解決它? (循環和字符串)
這是錯誤messege:
chr = str[i]
類型錯誤:字符串索引必須是整數,而不是str的
我的代碼是:
def CapsChanger(str):
i = str[0]
for i in str :
chr = str[i]
if((ord(chr) > 46) and (ord(chr) < 91)):
str[i].upper()
if((ord(chr) > 96) and (ord(chr) < 126)):
str[i].lower()
print str
str = raw_input()
CapsChanger(str)
input()
'for i in str'迭代實際的字符,無指示。不需要'chr = str [i]'。 –
這是什麼意思? – TotalyNotUbisoft