Home > Mobile >  getting the value on a list with a combined index
getting the value on a list with a combined index

Time:09-30

coded = ['','','','','','','','','','',
        'a','b','c','d','e','f','g','h','i',
        'j','k','l','m','n','o','p','q','r',
        's','t','u','v','w','x','y','z',' ']

def decode(decode):
     out = ''
     for x in range(1, len(str(decode)) // 2):
          out  = coded[int(str(decode[x * 2 - 2])   str(decode[x * 2 - 1]))]  #here is the issue
     return out

print(decode(16133335202320))

im trying to get a value from a list with every 2 characters from the input value. but, where ive commented, it keeps coming up with "int' object is not subscriptable".

how do i fix this?

CodePudding user response:

This answer is based on fischmalte's comment:

coded = ['','','','','','','','','','',
        'a','b','c','d','e','f','g','h','i',
        'j','k','l','m','n','o','p','q','r',
        's','t','u','v','w','x','y','z',' ']

def decode(decode):
    out = ''
    for x in range(1, len(str(decode)) // 2):
        out  = coded[int(str(decode)[x * 2 - 2]   str(decode)[x * 2 - 1])] ### EDITED LINE
    return out

print(decode(16133335202320))

Change str(decode[x * 2 - 2]) to str(decode)[x * 2 - 2] and str(decode[x * 2 - 1]) to str(decode)[x * 2 - 1].

CodePudding user response:

You have to convert your number to string before:

>>> print(decode(16133335202320))
...
TypeError: 'int' object is not subscriptable


#         HERE ---v
>>> print(decode(str(16133335202320)))
gdxzkn

OR

You can rewrite your function like below:

def decode(decode):
     decode = str(decode)
     out = ''
     for x in range(1, len(decode) // 2):
          out  = coded[int(decode[x * 2 - 2]   decode[x * 2 - 1])]
     return out
>>> print(decode(16133335202320))
gdxzkn
  • Related