I'm making an encryption tool in Python, and part of the process is to reassign each position with the encrypted position. The code is meant to run encryption on each character this is achieved through this code:
for pos in range (0, len(plaintext)-1):
print("pos is %s" % (pos))
The following code should be irrelevant but can be supplied. When this code is run, the output is,
pos is 0
pos is 1
The loop never reaches 2, despite the length of 'plaintext' being 3. If I were to remove the '-1' part (included as length and final position are not the same), I am met with an error, that the following code is out of range (the following code in my eyes doesn't need to be changed, however can be supplied).
Can anyone understand or explain my issue?
Python 2.7.11 Windows 8.1
EDIT:
The code following the loop start is as follows,
for pos in range (0, len(plaintext)-1):
print("pos is %s" % (pos))
for k in range (0, key):
if(plaintext[pos] == "z"):
crypt = "%s%s%s" % (crypt[:pos], "a", crypt[pos+1:])
else:
crypt = "%s%s%s" % (crypt[:pos], abc[abc.index(plaintext[pos])+1], crypt[pos+1:])
The error was raised due to the slicing being crypt[pos+1:]
because you cannot assign to a position in 2.7.11, so my workaround was to concatenate either 'side' of my position, around the newly encrypted key.