My code is implementing a Caesar Cipher, and this function is supposed to convert the inputted string, into an encrypted string.
To do this, I have to search for the character in 2 lists, a list of lowercase letters, uppercase letters, and then if it is not a letter, just add the character to the encrypted string.
I decided to use two layers of try's and except's to do this. Is there a better way I could do it, maybe with if/else?
import string from tkinter import * from tkinter import ttk alphaLower = string.ascii_lowercase alphaUpper = string.ascii_uppercase alphaShiftL = alphaLower alphaShiftU = alphaUpper def shiftList(amount): global alphaShiftL global alphaShiftU alphaShiftL = alphaLower[amount:] + alphaShiftL[:amount] alphaShiftU = alphaUpper[amount:] + alphaShiftU[:amount] def encrypt(unencrypted): encrypted = '' for char in unencrypted: #HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! try: alphaLower.index(char) encrypted += alphaShiftL[alphaLower.index(char)] except ValueError: try: encrypted += alphaShiftU[alphaUpper.index(char)] except ValueError: encrypted += char return encrypted
findinstead ofindexand avoid exceptions altogether.