0

I'm writing a very, very, very simple encryption script.

When I ran it, a TypeError occured:

TypeError: list indices must be integers, not str'

I looked through a few questions on Stackoverflow, but nothing helped.

I assume it's an extremly simple point, that I miss here.

import os

os.system("clear")

content = input("content:" + " ")
filename = input("filename:" + " ")

content = list(content)
file = open(filename, "w")

counter = 0

os.system("clear")

input("Press enter to encrypt...")

for counter in content:
    if content[counter] in "a":
        content[counter] = "z"
    if content[counter] in "b":
        content[counter] = "y"
    if content[counter] in "c":
        content[counter] = "x"
    if content[counter] in "d":
        content[counter] = "w"
    if content[counter] in "e":
        content[counter] = "v"
    if content[counter] in "f":
        content[counter] = "u"
    if content[counter] in "g":
        content[counter] = "t"
    if content[counter] in "h":
        content[counter] = "s"
    if content[counter] in "i":
        content[counter] = "r"
    if content[counter] in "j":
        content[counter] = "q"
    if content[counter] in "k":
        content[counter] = "p"
    if content[counter] in "l":
        content[counter] = "o"
    if content[counter] in "m":
        content[counter] = "n"
    if content[counter] in "n":
        content[counter] = "m"
    if content[counter] in "o":
        content[counter] = "l"
    if content[counter] in "p":
        content[counter] = "k"
    if content[counter] in "q":
        content[counter] = "j"
    if content[counter] in "r":
        content[counter] = "i"
    if content[counter] in "s":
        content[counter] = "h"
    if content[counter] in "t":
        content[counter] = "g"
    if content[counter] in "u":
        content[counter] = "f"
    if content[counter] in "v":
        content[counter] = "e"
    if content[counter] in "w":
        content[counter] = "d"
    if content[counter] in "x":
        content[counter] = "c"
    if content[counter] in "y":
        content[counter] = "b"
    if content[counter] in "z":
        content[counter] = "a"

content = "".join(content)

file.write(content)
file.close()

os.system("clear")

print("Successfully encrypted!")
print("Use 'decrypt.py' to decrypt.")
Reum12
  • 45
  • 4
  • 1
    When you are starting to hit Ctrl+V too much, take a step back.. in any case, *`counter` is a string* (representing the character being iterated) and `content[counter]` is thus invalid for the reason given in the exception message. – user2864740 Aug 26 '15 at 21:52
  • 1
    Put another way, run this: `for counter in content: print(counter)` (instead of the offending loop, with appropriate formatting added); what result do you see? Why? Use that observation to go back to the original problem. – user2864740 Aug 26 '15 at 21:55
  • 1
    If you want to iterate over indices of a sequence, use [`for i in range(0, len(content)): ..`](http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops). But this not really what is wanted (hint: strings are *not* mutable in Python and the individual characters of such can *not* be changed). Instead create a different variable say, `encryped_content = ""` and *append* the appropriate output to it each loop. Inside the loop you should only need to access `counter` (call it something else though, like `letter` to avoid confusion) and this `encryped_content`. – user2864740 Aug 26 '15 at 21:58

0 Answers0