The question says to concatenate using the ‘+’ operator. Then asks to modify print(encryption) so it returns ‘Encrypted text: mrttaqrhknsw ih puggrur’.
However this is shown as incorrect as the function has already ran therefore returning the encrypted text not the original ‘text’ variable. I have tried many,many ways before posting so i’m wondering if the question has been written incorrectly. thanks
Your code so far
text = 'mrttaqrhknsw ih puggrur'
custom_key = 'python'
def vigenere(message, key, direction=1):
key_index = 0
alphabet = 'abcdefghijklmnopqrstuvwxyz'
final_message = ''
for char in message.lower():
# Append any non-letter character to the message
if not char.isalpha():
final_message += char
else:
# Find the right key character to encode/decode
key_char = key[key_index % len(key)]
key_index += 1
# Define the offset and the encrypted/decrypted letter
offset = alphabet.index(key_char)
index = alphabet.find(char)
new_index = (index + offset*direction) % len(alphabet)
final_message += alphabet[new_index]
return final_message
def encrypt(message, key):
return vigenere(message, key)
def decrypt(message, key):
return vigenere(message, key, -1)
/* User Editable Region */
encryption = encrypt(text, custom_key)
print('Encrypted text: ' +encryption)
decryption = decrypt(encryption, custom_key)
print(decryption)
/* User Editable Region */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Learn String Manipulation by Building a Cipher - Step 83
‘mrttaqrhknsw ih puggrur’ is the text that is ran through the function to encrypt it, so the function returns the encrypted text as ‘bpmaodgfdugj xf ibutgsk’.
And erase the left code, in orde to use only the variable declared at this point “encryption”, the output is correct but … the answer it is not and i don’t know the reason
you need to use a variable in the print statement, the one with the value you want to print, not a function call
also, welcome to the forum, please always open a topic for your questions
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.