I've had a look around at older posts on this topic but can't see how to apply the knowledge to my code,
I'm trying to turn two functions into 1 as they are very similar but while trying to execute the function I get a:local variable 'z' referenced before assignment
error,
and here is the code:
code_a = ['h','e','l','o']
code_b = ['d','t','u','x']
def encrypt(word, a):
if a == 'encrypt':
z = code_a
y = code_b
elif a == 'decrypt':
z = code_b
y = code_a
newword = ''
for char in word:
x = 0
found = False
while found == False:
if z[x] == char:
newword = newword + y[x]
found = True
x += 1
return newword
x = encrypt('hello', 'encrypt')
print(x)
any help would be greatly appreciated thanks in advance
EDIT: After messing around with my code I figured out that the problem was the uncertainty of z
and 'y' so I got rid of the elif
statement and swapped the values in the if
statement and added an else
statement to make the default what the original if
statement was.