Search code examples
python-3.xcaesar-cipher

How to find middle of string to insert a word


I am trying to write a caesar cipher code, but make it harder than normal. The actual encryption is on a file that is then split into lines. For each line, I want to add a word to the beginning, middle, and end before doing the shift. So far I have this but it isn't working:

file = str(input("Enter input file:" ""))
my_file = open(file, "r")
file_contents = my_file.read()
#change all "e"s to "zw"s 
    for letter in file_contents:
        if letter == "e":
            file_contents = file_contents.replace(letter, "zw")
#add "hokie" to beginning, middle, and end of each line
    lines = file_contents.split('\n')
        def middle_message(lines, position, word_to_insert):
            lines = lines[:position] + word_to_insert + lines[position:]
            return lines
        message = "hokie" + middle_message(lines, len(lines)/2, "'hokie'") + "hokie"

I am getting

TypeError: slice indices must be integers or None or have an __index__ method

What am I doing wrong?I thought len() returns an int?


Solution

  • Assuming you are using python3

    You will need some more optimization even after fixing the len() not being an integer.

    Let's work this out first: our word for the example will be :

    a = 'abcdef'
    >>> len(a)
    6
    >>> len(a) / 2
    3.0 #this is your problem, dividing by 2 returns a float number
    

    The error you are getting is because float numbers (3.0 and 3.5) can't be used as slice indices in a list (or string), to solve this in this particular case:

    >>> len(a) // 2
    3
    

    Now, for optimization:

    This code takes a file, which I assume is made of many lines of text, since you are splitting your lines using '\n'.

    When you will have solved the slices part, you will get another error, telling you can't concatenate strings with lists (you should at this point, try your code with my proposed fix above, so you understand what happens)

    Your middle_message function is made to work with a single string, yet you are passing it your variable 'lines', which is a list of strings.

    to test I had to pass it an index of lines:

    message = "hokie" + middle_message(lines[0], len(lines[0])//2, "hokie") + "hokie"
    

    so if you want to work with all the strings in 'lines', a list comprehension loop could be useful, giving you a new list with your modified strings.

    newlines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]
    

    I know, this is longer than 80 characters.. you can work this down I'm sure ;)

    now here are the results I get by testing:

    >>> file_contents
    'This is going to be my test file\nfor checking some python cipher program.\ni want to see how it works.\n'
    
    >>> lines
    ['This is going to bzw my tzwst filzw', 'for chzwcking somzw python ciphzwr program.', 'i want to szwzw how it works.', '']
    
    >>> newlines
    ['hokieThis is going to hokiebzw my tzwst filzwhokie', 'hokiefor chzwcking somzw phokieython ciphzwr program.hokie', 'hokiei want to szwzhokiew how it works.hokie', 'hokiehokiehokie']