Python Forum

Full Version: cryptography help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(encode/decode sections) Hello, I am having troubles actually taking the letter i found in the key or the alpha and making it the new plain for the main functions. Every time when I try to run the program it just says must be str not int. Which does make any sense because the string is a str. Any thoughts?

""" crypto.py
    Implements a simple substitution cypher
"""

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key =   "XPMGTDHLYONZBWEARKJUFSCIQV"


def menu():
    print('   Secret Decoder Menu')
    print()
    print('   0)' 'Quit')
    print('   1)' 'Encode')
    print('   2)' 'Decode')
    print()
    response = input('What would you like to do? ')
    return response

# when the user tpyes a number it sends the answer beck to the main function where it can be used in the while loop


def encode(plain):
    new_word = " "
    for letter in plain.upper():
        if letter in alpha:
            letter.find(key)
            keyLetter = letter.find(key)
            plain = new_word + keyLetter
    return plain


def decode(coded):
    new_word = " "
    for letter in coded.upper():
        if letter in key:
            letter.find(alpha)
            keyLetter = letter.find(alpha)
            coded = new_word + keyLetter
    return plain



def main():
  keepGoing = True
  while keepGoing:
    response = menu()
    if response == "1":
      plain = input("text to be encoded: ")
      print(encode(plain))
      print()
    elif response == "2":
      coded = input("code to be decyphered: ")
      print (decode(coded))
      print()
    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...")

main()
not 100 percent sure...
on whatever it is you could put
str("""enter variable/info here""")
I would also try using ' instead of "
It would be most helpful to post your entire error traceback verbatim, no modifications
I also fixed your closing python tag, it was missing the '/'
The 'str.find()' is going to return a number (integer) starting with 0 For the beginning and ending with usually the length of the string, unless specified otherwise. It will be -1 if not found. You will need to rethink your logic to determine what exactly you want to do with the 'key' (index) as it relates to process.

Also, in your 'decoded' function, don't you want to return 'coded' rather than 'plain'?