sequence = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def caesar( original, variationShift):
'''returns: a version of original created by replacing each letter with
the letter "variationShift" units later in the alphabet sequence.
If variationShift is negative, replacing letter is found earlier in
alphabet.'''
index = 0
result = ''
while index < len( original):
lookFor = original[ index]
foundAt = sequence.find( lookFor)
if foundAt == -1:
# untranslatable character
result += lookFor
else:
result += sequence[ variationShift] # negative indexes OK!
index += 1
return result
def encryptCaesar (original):
caesar( original, foundAt - 3)
return result
def decryptCaesar (original):
caesar( original, (foundAt + 3) % len(sequence))
return result
The variable foundAt is constantly being said to be undefined. Yet, it is defined in caesar. Is a variable undefined until the function is actually run once? Or during that run?
foundAtis a local variable within thecaesarfunction. It's not visible outside the function.foundAtis only defined within the scope ofcaesar. It's not considered to exist anywhere else. So, the question is, do you really need to usefoundAtin those other methods? It wouldn't make sense to me that a cipher needs to know that much at that level.caesar, so you need it to have a value before it's set inside the function. What sense does that make?