Python Forum
Get the codemap combinations but got tuple index out of range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get the codemap combinations but got tuple index out of range (/thread-24589.html)



Get the codemap combinations but got tuple index out of range - haganmao - Feb-21-2020

my main purpose is to get the 4 combinations of codemap, and I got an error which is tuple index out of range, the code and error as below:
Code:

 def get_hash_key(self, long_url):

        
        code_map = []
  
        for v in range(0, 10):
            if v == 0:
                continue
            else:
                code_map.append(str(v))
        # not including letter “i, j, l, o”
        for v in range(97, 123):
            if v == 105:
                continue
            elif v == 106:
                continue
            elif v == 108:
                continue
            elif v == 111:
                continue
            else:
                code_map.append(chr(v))
        # not including letter“I, O”
        for v in range(65, 91):
            if v == 73:
                continue
            elif v == 79:
                continue
            else:
                code_map.append(chr(v))

      
        code_map = tuple(code_map)

        md5 = self.get_md5(long_url)
        su = []
        for i in range(0, 4):
            n = int(md5[i * 8:(i + 1) * 8], 16)
            v = [] 
            e = 0
            for j in range(0, 5):
                x = 0x0000003D & n
                e |= ((0x00000002 & n) >> 1) << j
                v.insert(0, code_map[x])
                n = n >> 6
            e |= n << 5
            v.insert(0, code_map[e & 0x0000003D])
            su.append(''.join(v))
        return su

    def get(self, *args, **kwargs):
        self.write(str(self.get_hash_key("http://www.google.com")))
error
Error:
Traceback (most recent call last): File "C:\Users\Meng\PycharmProjects\tornado-shorturl\venv\lib\site-packages\tornado\web.py", line 1697, in _execute result = method(*self.path_args, **self.path_kwargs) File "C:\Users\Meng\PycharmProjects\tornado-shorturl\app\views\views_common.py", line 68, in get self.write(str(self.get_hash_key("http://www.baidu.com"))) File "C:\Users\Meng\PycharmProjects\tornado-shorturl\app\views\views_common.py", line 60, in get_hash_key v.insert(0, code_map[x]) IndexError: tuple index out of range



RE: Get the codemap combinations but got tuple index out of range - michael1789 - Feb-21-2020

code_map is a tuple so its indexes are 0 and 1. But on line 44 you are calling for index x which is 0x0000003D & n. That is not in the range of 0 or 1.