while循环中的字符串比较

2024-10-01 19:21:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了一小段代码,可以检测两个字符串中同一位置是否有任何匹配的字符。如果有,则分数递增1,如果有2个或更多连续匹配字符,则分数递增3,如果没有匹配字符,则分数递减1。 但问题是,当我尝试运行代码时,它会给我一个错误:字符串索引超出范围。 怎么了?非常感谢你。

def pairwiseScore(seqA, seqB):
    count = 0
    score = 0
    while count < len(seqA):
        if seqA[count] == seqB[count]:
            score = score + 1
            count = count + 1
            while seqA[count] == seqB[count]:  # This is the line the error occurs
                score = score + 3
                count = count + 1 
        elif seqA[count] != seqB[count]:
            score = score - 1
            count = count + 1
    return score

Tags: the字符串代码lendefcount错误字符

热门问题