Python Forum

Full Version: Decrypt Caesar Cipher
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Good evening all,

I wondered if one of you may be able to help me, I'm using a resource at a website called 'Bourne to Code' (actual link = https://bournetocode.com/projects/8-CS-C...esson.html), however I can't figure it out and get a 'name, position is not defined' (I thought i'd done that!). Please could you tell me what I've done wrong? Any help would be very much appreciated.

alphabet='abcdefghijklmnopqrstuvwxyz'
cipherText='''odmhxf dmzqa, ljqruh wkrvh iluvw wzr zrugv wkhb duh mxvw wkhuh wr 
frqixvh d fudfnhu. dqbzdb wkh vhfuhw phvvdjh lv,
qrergb hashfwv wkh vsdqlvk lqtxlvlwlrq'''

for i in range (1,26): #loop through i
    plaintext='' #set plaintext to blank
for letter in cipherText: #for each letter in cypherText
    if letter !=alphabet: #if the letter is not in the alphabet
        shiftedLetter=letter #set the shiftedletter to the letter
    else:
        position=alphabet.index(letter) 
    shiftedIndex= position + i
    shiftedLetter = alphabet [shiftedIndex]
    plainText+=shiftedLetter
    print('With a shift of',i,' the message is \n\n', plainText,'\n')
Thank you.
Please post full error traceback in error code tags.
Like this?
>>> 
 
Traceback (most recent call last):
  File line 13, in <module>
    shiftedIndex= position + i
NameError: name 'position' is not defined
>>> 
Error tags instead of Python tags would be more suitable (icon next to Python tags, with red X), but it's alright.

You have defined position inside if-else. After that it gets out of scope, meaning the code that follows cannot access it. You should make position variable accessible to all the code by defining it outside for and if statements, like you did with alphabet and cipherText. Same will happen with plainText.
Thanks J.

However, now I'm not sure what to set the position variable to? is it along the lines of position=alphabet.index (I have no idea)?

I'm trying to work through the Bourne to code doc, but there are big holes as to what to do. It also mentions modulo and I cant't see where that would fit in at the moment either.
Alright I checked the code at the resource you provided. Code there is different from what you have:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
cipherText = '''odmhxf dmzqa, ljqruh wkrvh iluvw wzr zrugv wkhb duh mxvw wkhuh wr 
frqixvh d fudfnhu. dqbzdb wkh vhfuhw phvvdjh lv,
qrergb hashfwv wkh vsdqlvk lqtxlvlwlrq'''

for i in range(1, 26):
	plainText = ''
	for letter in cipherText:
		position = alphabet.index(letter)
		shiftedIndex = position + i
		shiftedLetter = alphabet[shiftedIndex]
		print(shiftedLetter)
As you see there is a nested for loop (for loop inside another for loop). That is due to indentation, which you don't have in your code, so it will by no chance behave same. In this code, the inner for loop has access to plainText variable.
Hi J,

Yes but it goes on to start adding more to the code to counter spaces and other characters. I've now changed the position variable to 1, and now the error is as follows:
Error:
Traceback (most recent call last): File , line 14, in <module> shiftedLetter = alphabet [shiftedIndex] IndexError: string index out of range
I don't think you need to change position variable. It is determined in each iteration by position of a letter in the alphabet. That is what the cipher algorithm should be doing, so that substitute (shifted) character can be determined.
OK, in which case, what do I need to set the variable to then, if I set it to position = alphabet.index then I get:
Error:
Traceback (most recent call last): File , line 13, in <module> shiftedIndex= position + i TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'int'
If I type in position=alphabet.index(letter) then I get:
Error:
Traceback (most recent call last): File , line 5, in <module> position = alphabet.index(letter) NameError: name 'letter' is not defined
Flumoxed!
I see the problem here, but in future posts please add code (in tags) which produced the errors, alongisde errors.

position = alphabet.index is not same as the original position = alphabet.index(letter).
The latter means position will be assigned a value, which is index of letter in the alphabet. While index() is a method (function) of string class, which is called on string alphabet.

There must be a change in code to get 2nd error, because letter is defined. It is a character(s) in cipherText, over which the loop iterates.
Pages: 1 2 3