无法解决错误"字符串索引超出范围"

时间:2018-01-27 20:48:10

标签: python

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)   

1 个答案:

答案 0 :(得分:0)

此行导致问题:while(i<=len(x))

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