【问题标题】:Unable to resolve the Error "string index out of range"无法解决错误“字符串索引超出范围”
【发布时间】:2018-01-27 20:48:09
【问题描述】:
import sys 
x=input("Enter the string")   `# getting the input#`
i=1 
ctr=0 


 print(len(x))                  #printing the length of the whole line
    while(i<=len(x)):        
         if(x[i]=='a'):             
            ctr=ctr+1                    
         i=int(i+1) 


 print(ctr)   

【问题讨论】:

  • 请在您的问题中添加更多信息,说明您要达到的目标,以便其他人可以提供帮助。
  • Python 使用从 0 开始的索引,所以你需要 i=0i&lt;len(x)

标签: python


【解决方案1】:

此行导致问题:while(i&lt;=len(x))

您应该删除=,因为字符串边界是 [0, len(x)-1]

【讨论】:

    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 2013-09-25
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多