简体   繁体   English

类型错误:字符串索引必须是整数

[英]Type Error: string indices must be integers

I'm very new to coding and as a school project I was trying to create a Cesar code, I got it working but then I tried to reverse it, so it could decode.我对编码很陌生,作为一个学校项目,我试图创建一个 Cesar 代码,我让它工作了,但后来我试图反转它,所以它可以解码。 I thought it was as simple as subtracting the jump but then it came up with this:我认为这就像减去跳跃一样简单,但后来想到了这个:

entrada=input("Please input your Message:")
matriz= "abcdefghijklmnñopqrstuvwyz 1234567890"

salida=''

for letra in entrada:
 posicion = matriz.find(letra.lower())
 salto = posicion
 posicion = posicion-salto

 if posicion > len(matriz) -1:
  posicion=posicion-len(matriz)

 if posicion < len(matriz) +1:
  posicion = posicion+len(matriz)

 salida = salida+matriz[posicion]


print (salida)

After this, it came back with the error in line 18在此之后,它返回第 18 行的错误

salto = posicion
posicion = posicion-salto

after this your posicion is 0 whatever salto and posicion you have had before.在此之后,无论您之前使用过什么盐和 posicion,您的 posicion 都是 0。

 if posicion < len(matriz) +1:
    posicion = posicion+len(matriz)

Here you get posicion which is greater than len(matriz), so matriz[position] results in IndexError在这里你得到大于 len(matriz) 的 posicion,所以 matriz[position] 导致 IndexError

Firstly,首先,

salto = posicion
posicion = posicion-salto

will make posicion 0, no matter what. posicion都会使posicion 0。 Perhaps, you're missing a piece of code here.也许,您在这里遗漏了一段代码。 Also, your code is returning IndexError: string index out of range at line 17, salida = salida + matriz[posicion] .此外,您的代码在第 17 行返回IndexError: string index out of range , salida = salida + matriz[posicion] There is no type error.没有类型错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM