Can't return index value for key in vigenere cipher

I am trying to complete the vigenere cipher assignment. I am encountering an error that I cannot figure out with these two lines:

k = key[i] % m
cipher_text = cipher_text + chr((ord(letter) - 97 + k) % 26 + 97)

The bottom line above will not process due to the “k” variable definition above it. I cannot figure out how to return the index value of my key. Currently running this code results in this error on the first line: “TypeError: not all arguments converted during string formatting”. I have tried using the int(), float(), and index() functions to no avail. Can anyone tell me what I am missing?

Assignment link: https://www.codecademy.com/paths/data-science-nlp/tracks/dsf-python-fundamentals-for-data-science-part-ii/modules/dsf-python-strings/projects/coded-correspondence-project

Does that represent an integer?

Hello! Thanks for taking a look.

It should represent an integer, yes; specifically it should be the integer value of the key’s index within the current loop of the function. But for some reason it is not being interpreted as one when I run my code and I’m not sure why.

Please show us how key is defined and updated?

The key is given as a string parameter in a function, and it should be getting updated with each iteration of a loop within the function. The function code I have so far is below:

def vigenere(key, message):
message = message.lower()
message = message.replace(’ ‘,’')
m = len(key)
cipher_text = ‘’
for i in range(len(message)):
letter = message[i]
k = key[i] % m
cipher_text = cipher_text + chr((ord(letter) - 97 + k) % 26 + 97)
return cipher_text

if key is a string, then it follows key[i] is also string. Can we compute the remainder on a string?

1 Like

Yes the key is a string, but with key[i] I am trying to represent the index value of the key variable that is active during the running loop iteration, i.e. if the key is “hello”, then on the first loop key[i] should return 0, and on the next loop key[i] should return 1, and so forth. I have tried to use multiple functions to return this value, such as int(), float(), and index(), but I cannot seem to find the right one. I have also tried converting the key to a list within the function to see if that would help, but it hasn’t.

Please explain, why and how?

1 Like

This feels fundamentally wrong: k = key[i] % m

Here’s a minimal test for it:

key = [1,2,3]
m = len(key)
for i in range(m * 2):
    k = key[i] % m
    print(i, k)

Result:

0 1
1 2
2 0
Traceback (most recent call last):
  File "t.py", line 4, in <module>
    k = key[i] % m
        ~~~^^^
IndexError: list index out of range

I strongly suspect you’re after something more like: k = key[i % m]

key = [1,2,3]
m = len(key)
for i in range(m * 2):
    k = key[i % m]
    print(i, k)
0 1
1 2
2 3
3 1
4 2
5 3

If key is a string, this should still spin for you: key = 'Pas'

Result:

0 P
1 a
2 s
3 P
4 a
5 s

Of course, if the key is a string, you’ll need to get a number out of it.

I strongly recommend throwing trace prints into your code so you can see what’s going on.

I agree on checking the code more frequently throughout with prints. I’ve decided to brainstorm this and re-examine my lessons/code to potentially go another route. Thanks for the assistance everyone!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.