首页 > 解决方案 > 索引时 int 对象不可下标

问题描述

我只是想编写一个简单的代码来找出具有最大相似元素数量的字符串,但是在将字符串的各个元素与目标字符串进行比较时,我得到了错误 int object is not subscriptable。编辑:错误在行中:如果 a[b]==t[b]: please help 。在代码中,我获取了一个字符串列表,然后将其与目标字符串字母表进行比较,并打印具有最多相似字母表(具有相同索引号)的字符串

n=int(input('enter the number of elements in the list'))
t=input('enter the target string')
l=[]
for x in range(0,n):
    st=input('enter the string')
    l.append (st)
length=len(t)
high=0
for a in l:
    score=0
    for b in range(0,length):
        if a[b]==t[b]:
            score+=1
    if score>high:
        high=score
        word=a
print('the word with the maximum score is :',word)

标签: pythonpython-3.xlistint

解决方案


您将整数附加到 l

for x in range(0,n):
    st=input('enter the string')
    l.append (x) #here

和你循环 l

for a in l:
    score=0
    for b in range(0,length):
        if a[b]==t[b]:  <--------- #here you say the b'th value of the integer a

整数不是数组,它只有一个值


推荐阅读