Nov-04-2018, 02:36 AM
(This post was last modified: Nov-04-2018, 02:36 AM by Drone4four.)
(Nov-02-2018, 05:35 PM)nilamo Wrote:>>> for index, value in enumerate(["spam", "cat", "fish", "bar"]): ... print(f"{index} => {value}") ... 0 => spam 1 => cat 2 => fish 3 => barWhat you're callingvariable
is actually the index inshifted
that thechar
is located. And because it's an index, it's anint
, so it's the same as...>>> x = 5 >>> x["a"] = [] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object does not support item assignment
I think I see where you are going. When referring to an index, the slicing call must be an integer whereas in my case I was slicing using a string. So I tried a few different integers in place of
char
at line 21 (like I separately tried, 0
, 1
,and -1
). That got rid of the 'int' object TypeError but now I've got a str error, described at the end of this forum post. Quote:list(some_thing)
will convertsome_thing
into a list, not a string. Yourtext
is already a string, you're converting it into a list (though it doesn't actually matter, since both lists and strings are iterable and indexable, so you should be able to just comment that line out).
I understand what you are saying about how it might not be necessary to cast my string into a list however strings are immutable. So I need to convert the string into a list so I can swap out the letters. The plan was to then at the end of these operations, join each item in the list of scrambled characters back together into string formatting.
I have played around with about 4 or 5 different combinations of
index
, char
and enumerate(text/shifted)
and here I am mashing my keyboard still unable to figure this out.Here is the closest I’ve come:
from collections import deque import string import copy def encrypt(text,shift_variance): ''' INPUT: text as a string and an integer for the shift value. OUTPUT: The shifted text after being run through the Caesar cipher. ''' original = string.ascii_lowercase # Initializing alphabet variable original = deque(list(original)) # Turning the original alphabet into a list shifted = original.copy() # Assigning new variable to copy of original alphabet shifted.rotate(shift_variance) # Rotating new shifted alphabet # BEGIN for loop: # text = list(text) # Convert text to string print(text) # Confirmation of conversion operation # scrambled_text =[] # Initializing output variable for index, value in enumerate(shifted): # for variable, char in enumerate(shifted): print(f"{index} => {value}") for index, character in enumerate(text): character[0] = the corresponding character in shifted_alphabet print(character) passAs you can see I’ve commented out the line where I cast the text string into a list (even though I think it is still necessary).
Lines 19-21 are there to demonstrate the point you made about enumeration except I’ve put the variables in the context of the variables and semantics in my script.
I feel like line 22 is the way it should be. At line 23 instead of slicing using the
character
(a string) I use an integer 0
. This does away with the Int object TypeError. But now Anaconda throws: SyntaxError: invalid syntax
. I get that line 23 is completely wrong. I can't for the life of me figure out how to re-write this pseudo code in Python code. Do I need to add a nested loop at line 23?
Thank you @nilamo for your help so far and thank you for your continued patience as I take my first baby steps in writing my first Python program.