首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python列表中的IndexError

python列表中的IndexError
EN

Stack Overflow用户
提问于 2016-05-12 16:17:33
回答 1查看 33关注 0票数 0

试图编写python代码来加密字符串。

加密字符串,输出为加密字符串。

代码语言:javascript
复制
print "Enter the string "
a=raw_input()
b=len(a)+1
i=0
e=''


while i<b:
  c=''
  if i % 3 == 0:
     c+=a[i]
     e+=chr(ord(c)+5)
     del c
  elif i%3==1:
     c+=a[i]
     e+=chr(ord(c)+2)
     del c
  elif i%3==2:
     c+=a[i]
     e+=chr(ord(c)+6)
     del c     
  i=i+1

print e 

但在运行此脚本时,会出现错误。

代码语言:javascript
复制
c+=a[i]
IndexError: string index out of range  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-12 16:26:54

问题是,当ilen(a)相等时,您的a[i]将生成IndexError

除了这个之外,还有很多其他的改进,比如您总是在执行c+=a[i],而不管条件如何,还有更多您应该自己尝试解决的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37192397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档