Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#12
(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 => bar
What you're calling variable is actually the index in shifted that the char is located. And because it's an index, it's an int, 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 convert some_thing into a list, not a string. Your text 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)
    pass
As 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.
Reply


Messages In This Thread
Caesar cipher - by Drone4four - Oct-26-2018, 03:06 PM
RE: Caesar cipher - by nilamo - Oct-26-2018, 03:12 PM
RE: Caesar cipher - by DeaD_EyE - Oct-26-2018, 07:48 PM
RE: Caesar cipher - by Drone4four - Oct-26-2018, 08:47 PM
RE: Caesar cipher - by stullis - Oct-27-2018, 12:20 AM
RE: Caesar cipher - by Drone4four - Oct-28-2018, 12:34 AM
RE: Caesar cipher - by knackwurstbagel - Oct-28-2018, 01:08 AM
RE: Caesar cipher - by stullis - Oct-28-2018, 01:47 AM
RE: Caesar cipher - by nilamo - Oct-28-2018, 03:43 AM
RE: Caesar cipher - by Drone4four - Nov-02-2018, 04:30 PM
RE: Caesar cipher - by nilamo - Nov-02-2018, 05:35 PM
RE: Caesar cipher - by Drone4four - Nov-04-2018, 02:36 AM
RE: Caesar cipher - by stullis - Nov-04-2018, 12:04 PM
RE: Caesar cipher - by nilamo - Nov-04-2018, 09:44 PM
RE: Caesar cipher - by Drone4four - Nov-06-2018, 02:13 AM
RE: Caesar cipher - by stullis - Nov-06-2018, 02:50 AM
RE: Caesar cipher - by Drone4four - Nov-09-2018, 07:22 PM
RE: Caesar cipher - by nilamo - Nov-09-2018, 07:50 PM
RE: Caesar cipher - by Drone4four - Nov-10-2018, 12:20 AM
RE: Caesar cipher - by nilamo - Nov-11-2018, 04:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cipher Caesar Azilkhan 1 2,162 Nov-21-2019, 03:40 PM
Last Post: ichabod801
  No idea how to use the Caesar Cypher in my code celtickodiak 5 3,119 Oct-08-2019, 03:29 AM
Last Post: stullis
  Monoalphabetic cipher pawlo392 1 12,841 Apr-01-2019, 08:51 PM
Last Post: ichabod801
  Vigenere and Caesar Cipher sammy2938 1 5,761 Jul-29-2017, 01:32 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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