0
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV" 

def encode():
    alpha[""] = key["x"]

def decode():
    key[""] = alpha[""] 

def menu():
    response = raw_input("""Crypto Menu
        quit(0)
        encode(1)
        decode(2)""")
    return response

def main():   
    keepGoing = True
    while keepGoing:
    response = menu()
    if response == "1":
        plain = raw_input("text to be encoded: ")
        print encode()
    elif response == "2":
        coded = raw_input("code to be decyphered: ")
        print decode()
    elif response == "0":
        print "Thanks for doing secret spy stuff with me."
        keepGoing = False
    else:
        print "I don't know what you want to do..."

print main()

I keep getting a TypeError saying string indices must be integers, not type. Not sure how to correct this, it is highlighting the decode and encode variables.

  • sorry first time posting on here, so formatting is still looking pretty bad – John Bennett Nov 02 '14 at 19:14
  • Welcome at StackOverflow, you should always use the programming language you are operating with as a tag. People tend to look for questions with certain tags and your post is more likely to be seen and answered if you specify your tags properly. – Robin Ellerkmann Nov 02 '14 at 19:24
  • Read the stack trace, figure out which line is raising the error, and include that in your question if you still can't figure out the problem. – khelwood Nov 02 '14 at 19:28
  • it is raising the error on line 6 – John Bennett Nov 02 '14 at 19:30

2 Answers2

0

I think your problem is here:

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV" 

def encode():
    alpha[""] = key["x"]

def decode():
    key[""] = alpha[""] 

I think you misunderstand how indexing in strings works. So let me try to correct this:

Take a string, like x = "hello". The reference x["h"] is meaningless. There's no way for Python to interpret this. On the other hand, x[0] is meaningful. It returns the element of x at index 0. That's "h", in our case.

Similarly, alpha[""] doesn't mean anything. When you use the square brackets, you are trying to specify an index in the string alpha. But the indices of alpha are integers. alpha[0] returns "A". alpha[1] returns "B". alpha[25] returns "Z".

So you need to use integers for your indices. Notation like key["x"] doesn't mean anything, and that raises errors.

Newb
  • 2,810
  • 3
  • 21
  • 35
  • That is definitely making it more understandable for me, however I am still unclear as to what should be put into the square brackets. the idea of the program is to ask the user whether they want to encode or decode a set of letters. the program will then use the alpha variable and transfer then into the key variable, and vice versa. so should i just simply put alpha[0] = key[0] and the opposite for the decode variable? will this tell the computer that each letter represents the letter in the same position for the two variable strings?(alpha and key) – John Bennett Nov 02 '14 at 19:54
0

There's a lot going on here, but you're definitely having issues with your encode and decode functions. If I understand what you're trying to do, you could rewrite them as follows:

def encode(string):
    alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    key = 'XPMGTDHLYONZBWEARKJUFSCIQV'
    encoded = []
    for character in string:
        character_index = alpha.index(character)
        encoded.append(key[character_index])
    return ''.join(encoded)

def decode(string):
    alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    key = 'XPMGTDHLYONZBWEARKJUFSCIQV'
    decoded = []
    for character in string:
        character_index = key.index(character)
        decoded.append(alpha[character_index])
    return ''.join(decoded)

Each function is doing essentially the same thing (here's what encode is doing:

  1. Creating an empty list called encoded. I'll use this to store each character, translated, in order.
  2. Loop through the characters of the string passed in.
  3. At each iteration, find its index in the string alpha.
  4. Find the character in key at that same index and append that character to the list encoded
  5. Once all the characters have been translated, join them into a string and return that string.

Note: This will fail if a character in the string argument is not found in the alpha string. You could add some error checking.

You could make this even more general if you wanted to allow for different keys. You could write a translate function like this:

def translate(string, from_language_string, to_language_string):
    translated = []
    for character in string:
        character_index = from_language_string.index(character)
        translated.append(to_language_string[character_index])
    return ''.join(translated)

And then your encode and decode functions could be written like this:

def encode(string):
    return translate(string, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'XPMGTDHLYONZBWEARKJUFSCIQV')

def decode(string):
    return translate(string, 'XPMGTDHLYONZBWEARKJUFSCIQV', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')

To address what's going on in the rest of your code, for the conditionals in your main function, you'll just want to make sure to pass the strings read in from raw_input to the encode and decode functions as needed. Something like this:

if response == '1':
    plain = raw_input('text to be encoded: ')
    print encode(plain)
    # and so on

Good luck.

Nicholas Flees
  • 1,943
  • 3
  • 22
  • 30
  • its definitely coming together but, ive not used the character_index = alpha.index(character) part before so it comes back as substring not found and im not sure why. any chance to explain that a bit more maybe? – John Bennett Nov 02 '14 at 20:26
  • Can you show me the context where that's coming up? Basically, it means that `character` isn't found in `alpha`. Could be because it's lowercase or something. Without seeing it, that would be my guess. – Nicholas Flees Nov 02 '14 at 20:28
  • Sure, when i go to run the program it allows me to choose whether to encode or decode and so i chose option 1(encode). I type the word to be encoded, trouble, and that is when it comes back with an error on line 6 of your section you displayed. it says ValueError substring not found – John Bennett Nov 02 '14 at 20:31
  • Yeah. Since "trouble" is lowercase, it's not finding them in the alpha string. – Nicholas Flees Nov 02 '14 at 20:31
  • ok so would i just added .uppercase to the raw_input line within the menu? – John Bennett Nov 02 '14 at 20:34
  • The method in Python is `.upper()` – Nicholas Flees Nov 02 '14 at 20:36
  • alright so i put the upper right after raw_input....so it looks like this raw_input.upper("""Crypto Menu etc.) says the function object has no attribute 'upper'. did i just put it in the wrong spot? – John Bennett Nov 02 '14 at 20:44
  • Yeah. You could do `plain = raw_input("Text to be encoded: ").upper()` – Nicholas Flees Nov 02 '14 at 20:58
  • I have one more thing to finish up on it, but i want to see if i can solve it myself – John Bennett Nov 02 '14 at 21:03
  • Excellent. Good luck. I would appreciate if you accept the answer if you feel it answered your question. Thanks. – Nicholas Flees Nov 02 '14 at 21:03