Tell us what’s happening:
text = ‘Hello Zaira’
custom_key = ‘python’
…
def vigenere(message, key, direction):
key_index = 0
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
encrypted_text = ‘’
…
return encrypted_text
encryption = vigenere(text, custom_key, direction)
print(encryption)
I get these errors and I am confused:
“NameError: name ‘direction’ is not defined”
“You should turn the last two lines in your code into comments”
Your code so far
# User Editable Region
text = 'Hello Zaira'
custom_key = 'python'
def vigenere(message, key):
key_index = 0
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''
for char in message.lower():
# Append space to the message
if char == ' ':
encrypted_text += char
else:
# Find the right key character to encode
key_char = key[key_index % len(key)]
key_index += 1
# Define the offset and the encrypted letter
offset = alphabet.index(key_char)
index = alphabet.find(char)
new_index = (index + offset) % len(alphabet)
encrypted_text += alphabet[new_index]
return encrypted_text
encryption = vigenere(text, custom_key, direction)
print(encryption)
# 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/130.0.0.0 Safari/537.36
Challenge Information:
Learn String Manipulation by Building a Cipher - Step 70
The “NameError: name ‘direction’ is not defined” is because you never defined the ‘direction’ variable that you are trying to pass as an argument and now you are trying to a pass variable that does not exist since you never defined it.
The directions for the problem are asking you to add a parameter to your function definition but you added argument to your function call instead.
A ‘parmeter’ is the named variable when you first define a function and an ‘argument’ is the named variable when you call that function that you defined when you are invoking or calling that function.
The difference between parameter and argument seems subtle but it matters because your code shows that the ‘direction’ variable is added to your function call as an argument but not your function definition as a parameter.
Also, when you define a function the number of parameters should usually match the number of arguments when you call that same function later.
Lastly, the directions want you to comment out the last two lines because you should have added a parameter to your function when defining it and now if you try to call it without passing the matching argument to the function call you are going to trigger errors.
Another way to put it is if you create or define a function where you are going to add two numbers ‘x’ and ‘y’
def addTwo(x, y):
return x + y
Then when you call that function your arguments need to exist and they need to be the same amount of arguments.
a = 2
b = 2
addTwo(a, b)
You can’t add an extra argument that you never defined
a = 2
b = 2
addTwo(a, b, c) # This will get you errors since you never defined 'c'
# and your function definition only has room for a and b
Thank you, I think I needed to take a break. I was struggling to see it. :pray
1 Like
I know the feeling. Sometimes you step away to take a break and when you come back it just makes sense.