我剛開始Python和我被困在一個愚蠢的錯誤,但我不明白爲什麼它不工作問:python:爲什麼我的char被視爲str?
def encryption(str1):
i = 0
for x in str1:
if (x >= 'a' and x <= 'z' or x >= 'A' and x <= 'Z'):
str1[i] = str1[i] + 3 % 26 #str[i] = x + 3 % 26
i+=1
return str1
當我執行該程序,我得到這個錯誤:
TypeError: string indices must be integers, not str.
燦有人向我解釋爲什麼str [i] - 23被認爲是str?對我來說,我只是修改了char的ascii值。
這不是C,嘗試'ORD(STR1 [I])'得到ASCII字符的值。爲什麼混合指數與'x'?並且錯誤似乎在其他地方... –
並在發佈之前修復您的縮進。 –
請記住,字符串是immutable –