Posts: 12
Threads: 1
Joined: Jan 2018
Jan-24-2018, 08:42 PM
(This post was last modified: Jan-25-2018, 09:04 AM by WantToImprove.)
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 08:58 PM
(This post was last modified: Jan-24-2018, 08:58 PM by j.crater.)
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.
Posts: 12
Threads: 1
Joined: Jan 2018
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.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 09:10 PM
(This post was last modified: Jan-24-2018, 09:10 PM by j.crater.)
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!
Posts: 12
Threads: 1
Joined: Jan 2018
Jan-29-2018, 03:35 PM
(This post was last modified: Jan-29-2018, 03:47 PM by WantToImprove.)
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-29-2018, 03:55 PM
(This post was last modified: Jan-29-2018, 03:55 PM by j.crater.)
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
Posts: 12
Threads: 1
Joined: Jan 2018
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?
Posts: 1,150
Threads: 42
Joined: Sep 2016
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
Posts: 12
Threads: 1
Joined: Jan 2018
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!
Posts: 1,150
Threads: 42
Joined: Sep 2016
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.
|