Python index error

I am new to programming and I am trying to write a Vigenère encryption cipher using python. The idea is very simple, and my function, however, is on this line:

( if((BinKey[i] == 'b')or(BinKey[i+1] == 'b')): ) 

It seems that I have a problem with the index, and I cannot figure out how to fix it. Error message:

 IndexError: string index out of range 

I tried replacing the index i+1 another variable equal to i+1 , since I thought that perhaps python would increment the value of i again, but it still wouldn't work.

So my questions are:

  • How to fix the problem and what did I do wrong?

  • Looking at my code, what can I learn to improve my programming skills?

  • I want to create a simple interface for my program (which will contain all the encryption ciphers), and all I came up with from Google is pyqt, but it just seems like too much work for a very simple interface, so is there an easier way to build interface? (I work with Eclipse Indigo and pydev with Python3.x)

Vigenère encryption function (which contains the string that causes the problem):

 def Viegner_Encyption_Cipher(Key,String): EncryptedMessage = "" i = 0 j = 0 BinKey = Bin_It(Key) BinString = Bin_It(String) BinKeyLengh = len(BinKey) BinStringLengh = len(BinString) while ((BinKeyLengh > i) and (BinStringLengh > j)): if((BinKey[i] == 'b')or(BinKey[i+1] == 'b')): EncryptedMessage = EncryptedMessage + BinKey[i] else: EncryptedMessage = EncryptedMessage + Xor(BinKey[i],BinString[j]) i = i + 1 j = j + 1 if (i == BinKeyLengh): i = i+j return EncryptedMessage 

This is the Bin_It function:

  def Bin_It(String): TheBin = "" for Charactere in String: TheBin = TheBin + bin(ord(Charactere)) return TheBin 

And finally, this is the Xor function:

 def Xor(a,b): xor = (int(a) and not int(b)) or (not int(a) and int(b)) if xor: return chr(1) else: return chr(0) 
+4
source share
4 answers

In your state, you guarantee that i < len(BinKey) . This means that BinKey[i] will be valid, but BinKey[i+1] will not be valid at the last iteration of your loop, since then you will get access to BinKey[len(BinKey)] , which is located at the end of the line. Lines in python start at 0 and end at len-1 inclusive.

To avoid this, you can update your loop criterion as

 while BinKeyLength > i+1 and ...: 
+4
source

You can either change

 while ((BinKeyLengh > i) and (BinStringLengh > j)): 

to

 while ((BinKeyLengh > i-1) and (BinStringLengh > j)): 

or change

 if((BinKey[i] == 'b')or(BinKey[i+1] == 'b')): 

to

 if((BinKey[i] == 'b') or (BinKeyLengh > i-1 and BinKey[i+1] == 'b')): 

This will avoid attempts to enter the BinKey[BinKeyLength] , which is beyond the scope.

+4
source

Looking at my code, what can I learn to improve my programming skills?

Quoting by index is not idiomatic Python. It is best to iterate over the elements of the iterator where possible. In the end, what usually interests you: for i in... often followed by my_list[i] .

In this example, you should use the built-in zip function (or itertools.izip if your code is lazy, although this code is not needed in Python 3), which gives you pairs of values ​​from two or more iterators and stops when the shortest one is exhausted.

 for key_char, string_char in zip(BinKey, BinString): # takes values sequentially from # BinKey and BinString # and binds them to the names # key_char and string_char # do processing on key_char and string_char 

If you really have to do a while in the index, then put the test in a different way so that it gives a clear idea of ​​what you are doing. Compare

 while len(BinKey) > i and len(BinString) > j: # this looks like len(BinKey) and # len(BinString) are varying and you're # comparing them to static variables i and j 

with

 while i < len(BinKey) and j < len(BinString): # this looks like you're varying i and j # and comparing them to len(BinKey) and len(BinString) 

What better links the goal of the cycle?


Finally, the sentence

 if (i == BinKeyLengh): i = i+j 

doesn't seem to be doing anything. If i == BinKeyLength , then the while will stop at any time.

+2
source

I think your mistake, as the Python interpreter says, is that you are accessing invalid positions in the array. To solve this problem, by contrast, you should change your code to

while (BinKeyLength > i+2 and ...):

This is because in the last step BinKeyLength = i + 2, then i + 1 is BinKeyLength-1, which is the last position of your array.

As for your programming skills, I recommend you two things:

  • Be the code. It sounds mystical, but the most important thing that is missing here is to find out what index numbers are used.
  • As rubik said, follow the style guidelines, such as the PEP8 style guide .
+1
source

Source: https://habr.com/ru/post/1445320/


All Articles