akhi27
akhi27

Reputation: 21

Error: list indices must be integers or slices, not tuple

Trying to read an input that coincides with abjadMapV. Then return the char in AbjadMap. But i keep getting this error.

def show(ln):

    abjadMapV=[1,2,3,4,5,6,7,8,9,
              10,20,30,40,50,60,70,80,90,
              100,200,300,400,500,600,700,800,900,
              1000,29]

    abjadMap=['\u0627','\u0628','\u062C','\u062F','\u0647','\u0648','\u0632','\u062D','\u0637',
          '\u064A','\u0643','\u0644','\u0645','\u0646','\u0633','\u0639','\u0641','\u0635',
          '\u0642','\u0631','\u0634','\u062A','\u062B','\u062E','\u0630','\u0636','\u0638',
          '\u063A','\uFEFC']
    abjadN=["alif","ba","jeem","dal","haa","waw","za","ha","da",
              "ya","kahf","laam","meem","noon","seen","ayn","fa","sadh",
              "qaf","ra","sheen","ta","tha","kha","thal","dhad","za",
              "gayn","lam alif"]

    i=0

    for i in enumerate(abjadMapV):
        if ln in abjadMapV[i] :
            print(i)
            print(abjadMap[i])
            return abjadMap[i]

    b=input()
    a=show(b)
    print(a)

Edited to new code trying to get show to return the index

def show(ln):

abjadMapV=[1,2,3,4,5,6,7,8,9,
          10,20,30,40,50,60,70,80,90,
          100,200,300,400,500,600,700,800,900,
          1000,29]

abjadMap=['\u0627','\u0628','\u062C','\u062F','\u0647','\u0648','\u0632','\u062D','\u0637',
      '\u064A','\u0643','\u0644','\u0645','\u0646','\u0633','\u0639','\u0641','\u0635',
      '\u0642','\u0631','\u0634','\u062A','\u062B','\u062E','\u0630','\u0636','\u0638',
      '\u063A','\uFEFC']
abjadN=["alif","ba","jeem","dal","haa","waw","za","ha","da",
          "ya","kahf","laam","meem","noon","seen","ayn","fa","sadh",
          "qaf","ra","sheen","ta","tha","kha","thal","dhad","za",
          "gayn","lam alif"]

i=0

for i in abjadMapV:

    if ln == i:
        return abjadMap.index(i)

b=input() a=show(b) print(a)

Upvotes: 0

Views: 650

Answers (2)

Param Siddharth
Param Siddharth

Reputation: 958

enumerate() returns a list of tuples, each being an index-value pair. You may destructure while iterating through them to solve the issue:

for i,j in enumerate(abjadMapV):
    if ln in j: # j is the value i. e. j = abjadMap[i] 
        print(i) # i is the index
        print(abjadMap[i] )
        return abjadMap[i] 

You may otherwise iterate through range(len(abjadMapV)) and use the variable as the index.

Upvotes: 0

Bibek Ghimire
Bibek Ghimire

Reputation: 552

The function enumerate returns tuple. So in line

print(abjadMap[i]) # i here is tuple.

And you have figured out that the list indices must be integer not Tuple. Therefore edit your code accordingly. If you are not familiar with enumerate function look at the example given below:

l=['a','b','c']
k=enumerate(l)

enumerate function returns iterable object: so k is an iterable object,

next(k)

gives the output:

(0,'a')

that means 0 is the index of a in list l.

for i in enumerate(l)

i is an tuple not an integer.

Upvotes: 1

Related Questions