Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decrypt Caesar Cipher
#11
Sorry, all should hopefully now be in correct format from hereon in.

With the following code:
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'''
position = alphabet.index
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')
I now get the error:
[error
Traceback (most recent call last):
line 13, in <module>
shiftedIndex= position + i
TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'int'[/error]

I made a change to the position + i part and changed it back and now with the code in the last post I get the error:

Error:
Traceback (most recent call last): line 13, in <module> shiftedLetter = alphabet [shiftedIndex] NameError: name 'shiftedIndex' is not defined
Reply
#12
We are back to having the 2nd for loop outside of the 1st. Unlike in original code, where it is nested. Indentation is not just stylistic, it makes a world of difference in Python code execution! ;) Lines that are indented inside the for loop will belong to that loop and execute in each iteration.

position = alphabet.index
This line does pretty much nothing useful. You are assigning a method to position variable (yeah, Python allows that! :D)

if letter !=alphabet: #if the letter is not in the alphabet
This line does not do what the comment says. It will return True if letter variable is same as alphabet variable, and False otherwise. See this.

About position, you want something like this:
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
        position = 0
        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')
But there are still mistakes and missing code, so there is more work to do.
Reply
#13
J, Thank you very much for your help, it is very much appreciated, however this has beat me, I'll knock it on the head I think as I'm going round in circles.

Again, thank you for your patience.
Reply
#14
You're welcome, I hope you will persist and solve the task, I think it is fun and educative!
Also it is not the most simple one, so don't get discouraged because of that. If you are just starting out with programming, I would advise to check some basic Python tutorials first, learn about data types, operations, loops, functions... After that this task will be much more simple, believe me ;)
Feel free to post on the forums when you hit an obstacle or have any kind of Python question!
Reply
#15
OK I'm back, I've had another go at it but again am stuck as to what I've done wrong!

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="" 
    position=0
    for letter in cipherText:
        if letter != alphabet:
            shiftedLetter=letter
        else:
            position=alphabet.index(letter)
        shiftedIndex=(position+i) % 26
        shiftedLetter=alphabet[shiftedIndex]
        plainText=plainText+shiftedLetter
        print(plainText)
Now I don't get an error but I do get each letter of the alphabet printed around 50 times! Any help would be great.

Ta
Reply
#16
Welcome back, I see nice progress towards the solution! ;)
First I believe your print call has an extra level of indentation, that you don't really want.
if letter != alphabet
This is not right. Here you check whether letter (string) is not equal to alphabet (string). Clearly none of the letters will equal entire alphabet. What you want to check is whether the letter/character is (not) in your alphabet, and you do this with:
if letter not in alphabet
Reply
#17
OK, so now the output is a long string of letters but only of the 26th pass. It also isn't picking up the spaces etc.
i assumed this:
if letter not in alphabet:
        shiftedLetter=letter
would set the shiftedLetter to a space (if it is a space!)

Also I don't understand why it isn't going through all 26 potential outputs and only shows the last?
Reply
#18
Quote:OK, so now the output is a long string of letters but only of the 26th pass. It also isn't picking up the spaces etc.
Ah, right, then forget my comment on print indentation. It looked too verbose to me and I assumed it is not what you really want. Especially since it's the last pass that gives the complete deciper attempt.

Pay some more attention to the indentations, particularly this part:
        else:
            position=alphabet.index(letter)
        shiftedIndex=(position+i) % 26
        shiftedLetter=alphabet[shiftedIndex]
        plainText=plainText+shiftedLetter
Reply
#19
Hi J!

This is now where I am:
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=''
    position=0
    for letter in cipherText:
        if letter not in alphabet:
            shiftedLetter=letter
        else:
            position =alphabet.index(letter)
            shiftedIndex = (position + i)%26
            shiftedLetter=alphabet[shiftedIndex]
            plainText=plainText+shiftedLetter
        print("With a shift of",i,'the message is\n\n',plainText,'\n')
Now my output goes like this:

Output:
With a shift of 2 the message is qfojzhfobscnlstwj With a shift of 2 the message is qfojzhfobscnlstwj With a shift of 2 the message is qfojzhfobscnlstwjy With a shift of 2 the message is qfojzhfobscnlstwjym With a shift of 2 the message is qfojzhfobscnlstwjymt With a shift of 2 the message is qfojzhfobscnlstwjymtx With a shift of 2 the message is qfojzhfobscnlstwjymtxj With a shift of 2 the message is qfojzhfobscnlstwjymtxj
All the way up to 26. How do I get it to output just so that it doesn't add a letter at a time? also why aren't the spaces appearing? and finally, i think I'm re-crypting the text even further as none of the outputs make sense, I therefore think I haven't got the shift correct (each letter shifts by one, then by two etc) I think it might be shifting the first letter by one the next by two etc.

Again, any help is appreciated!
Reply
#20
Problem is still in indentation at that particular part I pointed out in last post. Consider what each line does, and thus which lines should be inside else and which not. This will solve the logic part of the problem.

As for printing, to get only the last (26th) pass, you have unindented the print too much (by 2 levels). Unindent it just one level.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Rsa Cipher Paragoon2 3 578 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 453 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Transposition Cipher Issues beginner_geek07 0 1,046 Apr-08-2022, 07:44 PM
Last Post: beginner_geek07
  Encrypt and decrypt in python using own fixed key SriRajesh 3 4,626 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  Caesar Cipher Help pbrowne 2 2,112 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Learning Python with a Caesar cipher Drone4four 5 4,680 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Trying to encrypt and decrypt password into a file rpizw 4 3,211 Aug-12-2020, 05:15 PM
Last Post: bowlofred
  The code to decrypt Caeser Cipher. lazerwolf101 2 3,082 May-26-2020, 04:01 PM
Last Post: DT2000
  Coding caesar's cipher drewbty 3 2,720 May-16-2020, 10:05 AM
Last Post: DPaul
  cipher decryption tool nightfox82 0 1,301 Mar-25-2020, 06:36 AM
Last Post: nightfox82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020