import random
import time
import sys
import os
from string import digits
alphabet = [' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','~','`','!','@','#','$','^','&','*','_','=','/','<','>','?']
def menu_def():
menu = raw_input("[E]ncode or [D]ecode or [Q]uit:")
if menu == "e":
str1 = raw_input("Please enter the string you would like to encode:")
print "Please enter your secret variable (positive or negative int):"
secret = 0
while secret == 0:
secret = raw_input("")
if secret == 0:
print "Please do NOT choose 0 - it will cause problems"
str1 = str1.lower()
encoded = []
for letter in str1:
int_form = ord(letter) + int(secret)
str_form = random.choice(alphabet) + str(int_form) + random.choice(alphabet)
encoded.append(str_form)
encode_output = ""
for item in encoded:
encode_output = encode_output + "," + item
print "Here is your encoded string:"
encode_output = encode_output[1:]
print encode_output
menu_def()
elif menu == "d":
de_output = ""
print "Please enter the string you would like to decode:"
str2 = raw_input()
print "Please enter the secret variable the string was encoded with:"
secret = 0
while secret == 0:
secret = int(raw_input())
if secret == 0:
print "The string was not encoded with 0"
for digit in digits:
n = int(digit) - secret
item = chr(int(n))
de_output = de_output + item
print de_output
menu_def()
elif menu == "q":
sys.exit(0)
else:
print "Invalid Choice!"
menu_def()
menu_def()