0

I have a cipher called sortedcipher. And what I whats is to group 2 characters at once. At his point I have this code:

def Decrypt_Final():
    sortedcipher = "AVGFGAFFGFGFFAAAFGAVGAGAFFAAFAGFDFGDFFDXDFFDAGAAX"
    Mensagem_Original = ""
    i = 0
    while (i < (len(sortedcipher))):
        value = sortedcipher[i] + sortedcipher[i+1]
        for key in keysquare:
            if value in (keysquare[key]):
                Mensagem_Original += key
        i += 2
        value = ""
    return Mensagem_Original

Whats is doing is : "AV","GF"........"AA","X" and when it reach the end just gets the "X" instead of double letter because it cant do more sortedcipher[i+1].

How can I do a condition to stop do it when it reaches something like this?

Sanntozzz
  • 35
  • 5
  • Well, your loop ends with `i` being the index of the last element of `sortedcipher`. When that's the case what do you think `sortedcipher[i+1]` means? One solution could be to simply replace the loop stop condition with one less. – gspr Jan 13 '22 at 19:57
  • When the string has an odd number of characters what do you want the last iteration to be? – wwii Jan 13 '22 at 19:59
  • 3
    FYI, you can use a `for` loop for this, since `range()` allows you to specify the step size. `for i in range(0, len(sortedcipher)-1, 2):` – Barmar Jan 13 '22 at 20:03

0 Answers0