Posts: 12
Threads: 1
Joined: Jan 2018
Jan-24-2018, 07:32 PM
(This post was last modified: Jan-24-2018, 07:33 PM by WantToImprove.)
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.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Please post full error traceback in error code tags.
Posts: 12
Threads: 1
Joined: Jan 2018
Jan-24-2018, 07:57 PM
(This post was last modified: Jan-25-2018, 09:03 AM by WantToImprove.)
Like this?
>>>
Traceback (most recent call last):
File line 13, in <module>
shiftedIndex= position + i
NameError: name 'position' is not defined
>>>
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 08:04 PM
(This post was last modified: Jan-24-2018, 08:04 PM by j.crater.)
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.
Posts: 12
Threads: 1
Joined: Jan 2018
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.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 08:15 PM
(This post was last modified: Jan-24-2018, 08:15 PM by j.crater.)
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.
Posts: 12
Threads: 1
Joined: Jan 2018
Jan-24-2018, 08:17 PM
(This post was last modified: Jan-25-2018, 09:03 AM by WantToImprove.)
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 08:25 PM
(This post was last modified: Jan-24-2018, 08:25 PM by j.crater.)
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.
Posts: 12
Threads: 1
Joined: Jan 2018
Jan-24-2018, 08:30 PM
(This post was last modified: Jan-25-2018, 09:04 AM by WantToImprove.)
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!
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-24-2018, 08:35 PM
(This post was last modified: Jan-24-2018, 08:38 PM by j.crater.)
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.
|