The task is “use the addition assignment operator to increment key_index”. My answer was, ’ key_char += key_index’ and ‘key_index += key_char’.
Help, where did I got it wrong?
Your code so far
text = 'Hello Zaira'
custom_key = 'python'
def vigenere(message, key):
key_index = 0
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''
for char in message.lower():
# User Editable Region
# Append space to the message
if char == ' ':
encrypted_text += char
else:
key_char = key[key_index % len(key)]
key_char += key_index
# User Editable Region
index = alphabet.find(char)
new_index = (index + offset) % len(alphabet)
encrypted_text += alphabet[new_index]
print('plain text:', message)
print('encrypted text:', encrypted_text)
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0
Challenge Information:
Learn String Manipulation by Building a Cipher - Step 61
Hello, thanks for the reply! Sorry, still very new to python but I thought addition assignment operator is ‘+=’? Or it’s “key_index += key_char”? But I tried that so that can’t be right, did I miss something else entirely?
It appears you confused the key_index variable for key_char. Key_index was already declared in line 6 (key_index = 0). That’s what you need to increment by one.
If I have a variable for example:
ring_index = 0
print(ring_index) # prints out 0
ring_index += 1 # Increments ring_index by 1.
print(ring_index) # prints out 1
ring_index += 1 # Increments ring_index by 1.
print(ring_index) # prints out 2
The previous example of additional operator got me confused, finally caught up to the fact that I actually need to add that value into the variable. Thank you!
That is a very thorough explanation and I’m really grateful for that. I was very slow in understanding it but I finally got the answer right thank you!