-3

I have a list of special character:

specialCharList=['`','~','!','@','#','$','%','^',
                         '&','*','(',')','-','_','+','=',
                         '|','','{','}','[',']',';',':',
                         '"',',','.','<','>','/','?',"'",'\\',' ']

I need to include some sort of character notation for when the user presses the enter button for a new line. I've tried '\n' and that didn't work. Any ideas?

MORE INFO BELOW

Sorry, I should have specified. I'm making a basic encryption/decryption application. What it does is shifts the characters to the right 6 places and outputs that.

e.g. abc would output as ghi e.g. 123 would output as 789

I've done the same using special characters. Use the list below to see how it works.

specialCharList=['`','~','!','@','#','$','%','^',
                         '&','*','(',')','-','_','+','=',
                         '|','','{','}','[',']',';',':',
                         '"',',','.','<','>','/','?',"'",'\\',' ']

e.g. ~! would output as ^&

While everything works fine when someone inputs and combination of text,numbers, and special characters into the textbox to be encrypted, if someone inputs a new line (e.g. hits the enter key), I get an error.

index=specialCharList.index(tbInput[i])

ValueError: u'\n' is not in list

Full code is below.

import wx
import os
class mainForm(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Encryption Tool v2',size=(270,300))
        panel=wx.Panel(self)
        #Setting up controls
        wx.StaticText(panel,-1,'Enter Text Below',(10,10),(200,25))
        self.tbInput=wx.TextCtrl(panel,-1,'',(10,30),(250,220),wx.TE_MULTILINE)
        self.rdEncrypt=wx.RadioButton(panel,-1,'Encrypt',(10,250),(200,-1))
        self.rdDecrypt=wx.RadioButton(panel,-1,'Decrypt',(10,270),(200,-1))
        btnExecute=wx.Button(panel,-1,'Execute',(181,252),(80,-1))
        btnExecute.Bind(wx.EVT_BUTTON,self.encryptionDecryption)
    def encryptionDecryption(self,event):
        tbInput=self.tbInput.GetValue()
        rdEncrypt=self.rdEncrypt.GetValue()
        rdDecrypt=self.rdDecrypt.GetValue()
        if rdEncrypt==True and tbInput!='':
            #copy encryption code below
            encryptedStr=''
            alphabet=['X','M','y','B','e','f','N','D','i','Q',
                 'k','u','Z','J','s','A','q','Y','E','P','S',
                 'v','w','a','U','z','p','d','C','h','o','F',
                 'G','H','I','n','K','W','b','g','O','t','j',
                 'R','l','T','c','V','L','x','r','m']
            specialCharList=['`','~','!','@','#','$','%','^',
                             '&','*','(',')','-','_','+','=',
                             '|','','{','}','[',']',';',':',
                             '"',',','.','<','>','/','?',"'",'\\',' ',]
            for i in range(0,len(tbInput)):
                if tbInput[i].isalpha():
                    index=alphabet.index(tbInput[i])
                    if index+6>len(alphabet)-1:
                        index=5+(index-(len(alphabet)-1))
                        encryptedStr+=alphabet[index]
                    else:
                        encryptedStr+=alphabet[index+6]
                elif tbInput[i].isdigit():
                    if int(tbInput[i])+6>9:
                        encryptedStr+=str(-1+(int(tbInput[i])+6)-9)
                    else:
                        encryptedStr+=str(int(tbInput[i])+6)
                else:
                    index=specialCharList.index(tbInput[i])
                    if index+6>len(specialCharList)-1:
                        index=5+(index-(len(specialCharList)-1))
                        encryptedStr+=specialCharList[index]
                    else:
                        encryptedStr+=specialCharList[index+6]
            #print 'Encrypted Text: '+encryptedStr
            #text file here
            e=open('encryptedText.txt', 'w')
            e.write(encryptedStr)
            e.close()
            if os.name == 'nt':
                os.system('notepad ecryptedText.txt&')
            elif os.name == 'posix':
                os.system('gedit decryptedText.txt&')
            os.system('gedit encryptedText.txt&')
        elif rdDecrypt==True and tbInput!='':
            #copy code for decryption below
            decryptedStr=''
            alphabet=['X','M','y','B','e','f','N','D','i','Q',
                 'k','u','Z','J','s','A','q','Y','E','P','S',
                 'v','w','a','U','z','p','d','C','h','o','F',
                 'G','H','I','n','K','W','b','g','O','t','j',
                 'R','l','T','c','V','L','x','r','m']
            specialCharList=['`','~','!','@','#','$','%','^',
                             '&','*','(',')','-','_','+','=',
                             '|','','{','}','[',']',';',':',
                             '"',',','.','<','>','/','?',"'",'\\',' ']
            for i in range(0,len(tbInput)):
                if tbInput[i].isalpha():
                    index=alphabet.index(tbInput[i])
                    if index-6>len(alphabet)-1:
                        index=5+(index-(len(alphabet)-1))
                        decryptedStr+=alphabet[index]
                    else:
                        decryptedStr+=alphabet[index-6]
                elif tbInput[i].isdigit():
                    if int(tbInput[i])-6<0:
                        decryptedStr+=str(-1+(int(tbInput[i])-6)+11)
                    else:
                        decryptedStr+=str(int(tbInput[i])-6)
                else:
                    index=specialCharList.index(tbInput[i])
                    if index-6>len(specialCharList)-1:
                        index=5+(index-(len(specialCharList)-1))
                        decryptedStr+=specialCharList[index]
                    else:
                        decryptedStr+=specialCharList[index-6]
            #print 'Decrypted Text: '+decryptedStr
            #text file here
            d=open('decryptedText.txt', 'w')
            d.write(decryptedStr)
            d.close()
            if os.name == 'nt':
                os.system('notepad ecryptedText.txt&')
            elif os.name == 'posix':
                os.system('gedit decryptedText.txt&')
            os.system('gedit encryptedText.txt&')
        else:
            message=wx.MessageDialog(None, 'Please enter text for encryption/decryption','No Text Found',wx.OK|wx.ICON_INFORMATION)
            message.ShowModal()
            message.Destroy()
if __name__=='__main__':
    encryptionToolv2=wx.PySimpleApp()
    frame=mainForm(parent=None,id=-1)
    frame.Show()
    encryptionToolv2.MainLoop()
#usrInput=raw_input('Please enter your text.\n> ')
#eOrD=raw_input('Do you want to encrypt or decrypt? (e or d)\n> ')
#if eOrD=='e' or eOrD=='E':
#    encryption()
#elif eOrD=='d' or eOrD=='D':
#    decryption()
user1675111
  • 8,695
  • 4
  • 18
  • 14
  • 2
    `\n` *is* the notation for newline. Please show the piece of code that didn't work. – Fred Foo Sep 25 '12 at 14:30
  • 1
    What platform? And how do you capture the user keypresses? – Martijn Pieters Sep 25 '12 at 14:30
  • 5
    What do you mean with "that didn't work"? – orlp Sep 25 '12 at 14:30
  • Please try `print ('\ntext')`. Does this work? – phimuemue Sep 25 '12 at 14:30
  • 2
    At the very least, you'll have to specify *how* the user inputs anything. Terminal? (Which OS?) GUI Framework? (Which framework? Which widget?) –  Sep 25 '12 at 14:30
  • If you want a list of "special characters" surely it's everything that's not a-zA-z0-9? If you want a list of characters that mean special things upon user input isn't it whatever you decide? If you want to capture a newline then it's whatever the character is in whatever encoding you're using (normally 10 and/or 13). – Ben Sep 25 '12 at 14:31

2 Answers2

0

Most of textboxes(Mainly Windows) need \r\n to switch to the next line. \n alone is not sufficient.

user1655481
  • 376
  • 1
  • 10
0

I still think you should use a string for more readability. But still:

specialCharacters += ["\n", "\r", "\r\n", "\t"]

for new line, carriage return, carriage return and new line, tab.

Community
  • 1
  • 1
Pierre GM
  • 19,809
  • 3
  • 56
  • 67