Python中的字符串索引超出范围错误

Python中的字符串索引超出范围错误,python,string,indexing,Python,String,Indexing,当我尝试执行以下代码时,我不断收到“IndexError:string index out-range”错误消息: #function countLetters(word,letter) should count the number of times #a particular letter appears in a word. def countLetters(word, letter): count=0 wordlen=len(word) num=0

当我尝试执行以下代码时,我不断收到“IndexError:string index out-range”错误消息:

#function countLetters(word,letter) should count the number of times
#a particular letter appears in a word.

def countLetters(word, letter):

    count=0

    wordlen=len(word)
    num=0
    wordletter=""

    while(num<=wordlen):
        wordletter=word[num]
        if(wordletter==letter):
            count=count+1
        num=num+1
    return count

print(countLetters("banana", "x"))#should print 0
print(countLetters("banana", "a"))#should print 3
print(countLetters("banana", "b"))#should print 1
#函数countLetters(单词、字母)应计算次数
#某个字母出现在一个单词中。
def计数字母(单词、字母):
计数=0
单词len=len(单词)
num=0
wordletter=“”

而(num你把它看得太远了:

while(num<=wordlen):
while(num<=wordlen):

因为Python序列是基于0的。长度为5的字符串有索引0、1、2、3和4,而不是5。

你把它作为一个索引太过分了:

while(num<=wordlen):
while(num<=wordlen):

因为Python序列是基于0的。长度为5的字符串具有索引0、1、2、3和4,而不是5。

您到达一个索引太远了:

while(num<=wordlen):
while(num<=wordlen):

您达到一个索引的距离太远:

while(num<=wordlen):
while(num<=wordlen):

因为字符串的索引从零开始,最高有效索引将是wordlen-1。

因为字符串的索引从零开始,最高有效索引将是wordlen-1。

索引从0开始,而不是1

尝试更改:

wordletter = word[num-1]

索引从0开始,而不是从1开始

尝试更改:

wordletter = word[num-1]
修复您的代码:

def countLetters(word, letter):

    count=0

    wordlen=len(word)
    num=0
    wordletter=""
    #here, because the index access is 0-based
    #you have to test if the num is less than, not less than or equal to length
    #last index == len(word) -1
    while(num<wordlen):
        wordletter=word[num]
        if(wordletter==letter):
            count=count+1
        num=num+1
    return count

print(countLetters("banana", "x"))#should print 0
print(countLetters("banana", "a"))#should print 3
print(countLetters("banana", "b"))#should print 1
修复您的代码:

def countLetters(word, letter):

    count=0

    wordlen=len(word)
    num=0
    wordletter=""
    #here, because the index access is 0-based
    #you have to test if the num is less than, not less than or equal to length
    #last index == len(word) -1
    while(num<wordlen):
        wordletter=word[num]
        if(wordletter==letter):
            count=count+1
        num=num+1
    return count

print(countLetters("banana", "x"))#should print 0
print(countLetters("banana", "a"))#should print 3
print(countLetters("banana", "b"))#should print 1

在我看来,正在运行的代码和您正在查找的代码是不同的。在您的源代码中没有
var=word[num]
,而
回溯
似乎认为存在is@shaktimaan:它足够近了;它不是
var
而是
wordletter=word[num]
.FYI,我不知道这是否是用于分配,但你可以始终使用
.count()
。示例:
“香蕉”。count(“a”)
返回
3
。是的,我正在上课,这是一个作业。在我看来,正在运行的代码和正在查找的代码是不同的。没有
var=word[num]
在您的源代码中,而
回溯
似乎认为存在is@shaktimaan:它足够接近了;它不是
var
,而是
wordletter=word[num]
。仅供参考,我不知道这是否用于分配,但您始终可以使用
.count()
。例如:
香蕉.count(“a”)
返回
3
。是的,我正在上课,这是一项作业。@f.rodriques问题显示代码从索引0开始。@f.rodriques问题显示代码从索引0开始。非常感谢!我现在感觉非常愚蠢,但至少我知道我做错了什么!我非常感谢你,我一直在努力解决这个问题过去的两天!谢谢!非常感谢!我现在觉得自己真的很愚蠢,但至少我知道我做错了什么!我真是太感谢你了,过去两天我一直在努力想办法解决这个问题!谢谢!