Related
I have written a function which takes an integer and returns a dictionary where the letters of the alphabet are mapped to an alphabet shifted by the integer. It has the requirement that lowercase and uppercase letters must correpsond, that is if a -> b then A->B.
I'm fairly new to python and have little experience with creating lists so I'm just wondering if there is an alternative more efficient or even just more elegant way to complete the same outcome.
import string
dictionary = []
for letter in string.ascii_lowercase + string.ascii_uppercase:
dictionary.append(letter)
def build_shift(shift):
shifted_dictionary = []
for i in range(0,26):
shifted_dictionary.append(dictionary[(i + shift) % 26])
for i in range(26, 53):
shifted_dictionary.append(dictionary[(( i + shift) % 26) + 26])
return (shifted_dictionary)
mapped_dictionary = dict( zip(dictionary, build_shift(2)))
print(mapped_dictionary)
You can use a dict comprehension that iterates characters over each of the lowercase and uppercase strings to output the character mapped to the one at the current index plus the offset and reduced with a modulo of 26. Below is an example using 2 as the offset:
{
c: s[(i + 2) % 26]
for s in (string.ascii_lowercase, string.ascii_uppercase)
for i, c in enumerate(s)
}
You can slice the string and get the desired result in less number of operations.
import string
shift = 2
changed_lc = f'{string.ascii_lowercase[shift:]}{string.ascii_lowercase[:shift]}'
changed_up = f'{string.ascii_uppercase[shift:]}{string.ascii_uppercase[:shift]}'
changed = f'{changed_lc}{changed_up}'
mapped_dictionary = dict(zip(string.ascii_letters, changed))
print(mapped_dictionary)
output:
{'a': 'c', 'b': 'd', 'c': 'e', 'd': 'f', 'e': 'g', 'f': 'h', 'g': 'i', 'h': 'j', 'i': 'k', 'j': 'l', 'k': 'm', 'l': 'n', 'm': 'o', 'n': 'p', 'o': 'q', 'p': 'r', 'q': 's', 'r': 't', 's': 'u', 't': 'v', 'u': 'w', 'v': 'x', 'w': 'y', 'x': 'z', 'y': 'a', 'z': 'b', 'A': 'C', 'B': 'D', 'C': 'E', 'D': 'F', 'E': 'G', 'F': 'H', 'G': 'I', 'H': 'J', 'I': 'K', 'J': 'L', 'K': 'M', 'L': 'N', 'M': 'O', 'N': 'P', 'O': 'Q', 'P': 'R', 'Q': 'S', 'R': 'T', 'S': 'U', 'T': 'V', 'U': 'W', 'V': 'X', 'W': 'Y', 'X': 'Z', 'Y': 'A', 'Z': 'B'}
I have a problem for school and I can't seem to figure it out. Basically, i'm in an intro to object oriented programming class, so I only need to complete this as basic as possible without using anything fancy that I haven't learned yet. Currently learning about dictionaries and sets, I need to use a dictionary that has a code written in it to encrypt a document that has a long string on one line.
So I need one part to read the dictionary and open the text file containing the string.
"The course Introduction to Object Oriented Programming uses the Python programming language."
I need to then use the code from this dictionary to encrypt it and write the encrypted version of the string to another text file called encrypt.txt.
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '#', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'#': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
This is the code I have so far. Any help would be greatly appreciated and an explanation in layman's terms would also be greatly appreciated.
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '#', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'#': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
def main():
#Open the file you want to encrypt.
infile = str(input('Enter the name of the input file: '))
#read its contents
dtext = open(infile, 'r')
#read the line from the file
dtext = dtext.readlines()
#strip the newline
#dtext = dtext.rstrip('\n')
#call the encryptText function
encryptText(dtext)
def encryptText(dtext):
#enter the name of the file to write to
outfile = str(input('Enter the name of the output file: '))
#open the file to send encrypted text to
etext = open(outfile, 'w')
#set accumulator value
count = 0
#create a for loop to read each separate character
for line in dtext:
wordList = line.split()
print(dtext, CODE[dtext])
count += 1
main()
You need to encrypt character by character and you need to take the result and build it back into a string. str.join turns a sequence of characters into a string and a generator can be written to encrypt each character... put them together and you have your solution.
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '#', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'#': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
def main():
#Open the file you want to encrypt.
infile = str(input('Enter the name of the input file: '))
#read its contents
dtext = open(infile, 'r')
#read the line from the file
dtext = dtext.readlines()
#strip the newline
#dtext = dtext.rstrip('\n')
#call the encryptText function
encryptText(dtext)
def encryptText(dtext):
#enter the name of the file to write to
outfile = str(input('Enter the name of the output file: '))
#open the file to send encrypted text to
etext = open(outfile, 'w')
#set accumulator value
#create a for loop to read each separate character
for line in dtext:
# encrypt character by character then join to a string
encrypted = ''.join(CODE.get(c, c) for c in line)
print(repr(line), repr(encrypted))
etext.write(encrypted)
etext.close()
main()
Strings are immutable. That means you cannot edit them after creation, in direct contrast to arrays. You will have to build a new string in order to encrypt your text. You will also likely need to do this one character at a time. Since you have the text of the file in dtext as a string, you can loop through the chars in the original string like so:
for i in range (0, len(dtext)):
# add new character to string
(I'm breaking this apart so you cannot just copy and paste)
You must create a new string to put the encrypted text in outside of that for loop.
enc = ""
In order to encrypt the value by making a substitution you can add character one at a time to the encrypted string in that for loop if they are in the dictionary you defined. Something to the effect of
if (dtext[i] in CODE.keys()):
enc += CODE[dtext[i]]
Then write the new string to a file and you're good to go.
A dictionary in python is effectively an array that is indexable by a key. This key maps to a given value just like an array index maps to some value. See https://www.tutorialspoint.com/python/python_dictionary.htm for more details.
tdelaney's answer was correct, but I took it and made it a more simpler version. I changed it a bit. This is what I did:
Instead of joining to an empty string, I just removed that part completely. I just iterated over the entire string character by character and then as tdelaney did, used the get method to use the key in the code dictionary.
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '#', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'#': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
def main():
#Open the file you want to encrypt.
infile = str(input('Enter the name of the input file: '))
#read its contents
dtext = open(infile, 'r')
#read the line from the file
dtext = dtext.readlines()
#call the encryptText function
encryptText(dtext)
def encryptText(dtext):
#enter the name of the file to write to
outfile = str(input('Enter the name of the output file: '))
#open the file to send encrypted text to
etext = open(outfile, 'w')
#create a for loop to read each separate character
for line in dtext:
# encrypt character by character then join to a string
for c in line:
encrypted = (CODE.get(c, c))
etext.write(encrypted)
#close the file
etext.close()
main()
I got an assignment yesterday about Rot and I was told to write all 26 of them in code, or atleast a program that did, so I thought I'd start with the dictionary, since I wanted to also call for a certain rot I came up with this.
def generate_rotated_dictionary(n):
for i in range(0, 26):
letter = alphabet_list[i]
new_let = ord(letter) + n
new_let = chr(new_let)
rot_dic.update({letter: new_let})
return(rot_dic)
Unfortunately, this doesn't work and results in:
{'a': 'b', 'c': 'd', 'b': 'c', 'e': 'f', 'd': 'e', 'g': 'h', 'f': 'g', 'i':
'j', 'h': 'i', 'k': 'l', 'j': 'k', 'm': 'n', 'l': 'm', 'o': 'p', 'n': 'o',
'q': 'r', 'p': 'q', 's': 't', 'r': 's', 'u': 'v', 't': 'u', 'w': 'x', 'v':
'w', 'y': 'z', 'x': 'y', 'z': '{'}
{'a': 'c', 'c': 'e', 'b': 'd', 'e': 'g', 'd': 'f', 'g': 'i', 'f': 'h', 'i':
'k', 'h': 'j', 'k': 'm', 'j': 'l', 'm': 'o', 'l': 'n', 'o': 'q', 'n': 'p',
'q': 's', 'p': 'r', 's': 'u', 'r': 't', 'u': 'w', 't': 'v', 'w': 'y', 'v':
'x', 'y': 'z', 'x': 'z', 'z': '|'}
{'a': 'd', 'c': 'f', 'b': 'e', 'e': 'h', 'd': 'g', 'g': 'j', 'f': 'i', 'i':
'l', 'h': 'k', 'k': 'n', 'j': 'm', 'm': 'p', 'l': 'o', 'o': 'r', 'n': 'q',
'q': 't', 'p': 's', 's': 'v', 'r': 'u', 'u': 'x', 't': 'w', 'w': 'z', 'v':
'y', 'y': '|', 'x': 'z', 'z': '}'}
{'a': 'e', 'c': 'g', 'b': 'f', 'e': 'i', 'd': 'h', 'g': 'k', 'f': 'j', 'i':
'm', 'h': 'l', 'k': 'o', 'j': 'n', 'm': 'q', 'l': 'p', 'o': 's', 'n': 'r',
'q': 'u', 'p': 't', 's': 'w', 'r': 'v', 'u': 'y', 't': 'x', 'w': 'z', 'v':
'z', 'y': '}', 'x': '|', 'z': '~'}
How do I fix it with the ending, I know that it is because of my use of Ascii values, but how do i get this to start with the a again (number 97)
So if you need to rotate alphabet you can use following method:
import string
alph = string.ascii_lowercase
for i in range(26):
print(''.join([alph[(k + i) % 26] for k in range(26)]))
Output is alphabetic strings with offset:
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
.....
zabcdefghijklmnopqrstuvwxy
If you need lists instead of strings just remove join().
If you required some specific rotation like 13 then you can use this code inside function with argument:
def rot(i):
return ''.join([alph[(k + i) % 26] for k in range(26)])
This will give rotation for specific number.
Example:
print(rot(13))
Output:
nopqrstuvwxyzabcdefghijklm
l = list('abcdefghijklmnopqrstuvwxyz')
n = 1
rot = dict((c, chr(97 + ((ord(c)-97 + n)%26) )) for c in l)
The comprehension there is a little dense, so lets break it down:
(c, chr(97 + ((ord(c)-97 + n)%26) ))
produces tuples of one character and its shifted counterpart
chr(97 + ((ord(c)-97 + n)%26) )
makes a character
97 + (ord(c)-97 + n)%26
get the value of the old character and calculate how many to shift over.
So, in order to summarize, here's a full method:
# Generates a function which rotate a sequence by an offset
def generate_rot(seq, offset):
l = len(seq)
def rot(c):
n = (seq.index(c) + offset) % l
return seq[n]
return rot
> alpha = map(chr,range(97, 123)) # [a...z]
> rot13 = generate_rot(alpha, 13)
> print(rot13('a'))
n
> print(rot13('n'))
a
> rot21 = generate_rot(alpha, 21)
v
You can then bind it to the alphabet, specifically:
> def generate_alpha_rot(x):
return generate_rot(map(chr,range(97, 123)),x)
# or using a lambda function to create it inline, because why not?
> generate_alpha_rot = lambda x:generate_rot(map(chr,range(97, 123)),x)
> rot22 = generate_alpha_rot(22)
> rot22('a')
w
# Or call it inline:
> generate_alpha_rot(23)('a')
x
Hardly worth a full answer, but you want to take the modulo 26 of new_let before you convert to a character. by doing remainder division, you get how far past 26 the index has gone
Consider:
>>>26 + 13 # rot13 for z
39
>>>39 % 26
13
I am trying to make a dict that can apply a Caesar Cypher to a letter.
I need it to be in one dict for upper and lower but cannot figure put how to get both into one dict.
import string
def Coder(shift):
alpha = string.ascii_lowercase
ALPHA = string.ascii_uppercase
if shift in range(0,26):
return dict(zip(ALPHA, ALPHA[shift:] + ALPHA[0:shift])), dict(zip(alpha, alpha[shift:] + alpha[0:shift]))
Something like this:
import string
def Coder(shift):
alpha = string.ascii_lowercase
ALPHA = string.ascii_uppercase
if 0 <= shift < 26:
unshifted_letters = ALPHA + alpha
shifted_letters = ALPHA[shift:] + ALPHA[:shift] + alpha[shift:] + alpha[:shift]
return dict(zip(unshifted_letters, shifted_letters))
But as others have said, better solutions are encode('rot13') and string.maketrans. In particular, this: "rot_13 rot13 Unicode string Returns the Caesar-cypher encryption of the operand".
If you're new to programming, avoid trying to "do everything on one line".
string.index( substring ) finds the position of substring in string.
a % b takes the remainder of a divided by b.
string.upper() returns an uppercased version of string.
Knowing this, you should be able to understand every line in this program:
import string
translation = {}
shift = 5
alphabet = string.ascii_lowercase
for letter in alphabet:
position = alphabet.index( letter )
new_position = (position + shift) % len( alphabet )
translation[ letter ] = alphabet[ new_position ]
translation[ letter.upper() ] = alphabet[ new_position ].upper()
You can use dict.update():
First create a dictionary of uppercase letters and then update that dict with a dictionary of lowercase letters:
In [8]: from string import *
In [9]: al=ascii_lowercase
In [10]: au=ascii_uppercase
In [11]: for shift in range(2):
dic1=dict(zip(au, au[shift:] + au[0:shift]))
dic1.update(dict(zip(al, al[shift:] + al[0:shift])))
print dic1
....:
{'A': 'A', 'C': 'C', 'B': 'B', 'E': 'E', 'D': 'D', 'G': 'G', 'F': 'F', 'I': 'I', 'H': 'H', 'K': 'K', 'J': 'J', 'M': 'M', 'L': 'L', 'O': 'O', 'N': 'N', 'Q': 'Q', 'P': 'P', 'S': 'S', 'R': 'R', 'U': 'U', 'T': 'T', 'W': 'W', 'V': 'V', 'Y': 'Y', 'X': 'X', 'Z': 'Z', 'a': 'a', 'c': 'c', 'b': 'b', 'e': 'e', 'd': 'd', 'g': 'g', 'f': 'f', 'i': 'i', 'h': 'h', 'k': 'k', 'j': 'j', 'm': 'm', 'l': 'l', 'o': 'o', 'n': 'n', 'q': 'q', 'p': 'p', 's': 's', 'r': 'r', 'u': 'u', 't': 't', 'w': 'w', 'v': 'v', 'y': 'y', 'x': 'x', 'z': 'z'}
{'A': 'B', 'C': 'D', 'B': 'C', 'E': 'F', 'D': 'E', 'G': 'H', 'F': 'G', 'I': 'J', 'H': 'I', 'K': 'L', 'J': 'K', 'M': 'N', 'L': 'M', 'O': 'P', 'N': 'O', 'Q': 'R', 'P': 'Q', 'S': 'T', 'R': 'S', 'U': 'V', 'T': 'U', 'W': 'X', 'V': 'W', 'Y': 'Z', 'X': 'Y', 'Z': 'A', 'a': 'b', 'c': 'd', 'b': 'c', 'e': 'f', 'd': 'e', 'g': 'h', 'f': 'g', 'i': 'j', 'h': 'i', 'k': 'l', 'j': 'k', 'm': 'n', 'l': 'm', 'o': 'p', 'n': 'o', 'q': 'r', 'p': 'q', 's': 't', 'r': 's', 'u': 'v', 't': 'u', 'w': 'x', 'v': 'w', 'y': 'z', 'x': 'y', 'z': 'a'}
or you can also use str.translate() with string.maketrans:
for shift in xrange(4):
t=maketrans(au+al,au[shift:]+au[:shift]+al[shift:]+al[:shift])
print "abcxyzABCXYZ".translate(t)
....:
abcxyzABCXYZ
bcdyzaBCDYZA
cdezabCDEZAB
defabcDEFABC
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring in the
optional argument deletechars are removed, and the remaining
characters have been mapped through the given translation table, which
must be a string of length 256 or None. If the table argument is None,
no translation is applied and the operation simply removes the
characters in deletechars.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Caesar’s Cipher using python, could use a little help
Alright, so in my class, I'm supposed to write code that shifts a dictionary as a Ceasar cipher. My code shifts everything fine, but returns the keys in a different order than the provided test case.
My code:
sLowerCase = string.ascii_lowercase
sUpperCase = string.ascii_uppercase
dCode = {'A':'A', 'B':'B', 'C':'C', 'D':'D', 'E':'E', 'F':'F', 'G':'G', 'H':'H', 'I':'I', 'J':'J', 'K':'K', 'L':'L', 'M':'M', 'N':'N', 'O':'O', 'P':'P', 'Q':'Q', 'R':'R', 'S':'S', 'T':'T', 'U':'U', 'V':'V', 'W':'W', 'X':'X', 'Y':'Y', 'Z':'Z',
'a':'a', 'b':'b', 'c':'c', 'd':'d', 'e':'e', 'f':'f', 'g':'g', 'h':'h', 'i':'i', 'j':'j', 'k':'k', 'l':'l', 'm':'m', 'n':'n', 'o':'o', 'p':'p', 'q':'q', 'r':'r', 's':'s', 't':'t', 'u':'u', 'v':'v', 'w':'w', 'x':'x', 'y':'y', 'z':'z'}
for c in dCode.keys():
if c in sUpperCase:
if sUpperCase.index(c) + shift > 25:
dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) - 26]
else:
dCode[c] = sUpperCase[(sUpperCase.index(c) + shift)]
if c in sLowerCase:
if sLowerCase.index(c) + shift > 25:
dCode[c] = sLowerCase[(sLowerCase.index(c) + shift) - 26]
else:
dCode[c] = sLowerCase[(sLowerCase.index(c) + shift)]
return dCode
My output for buildCoder(0) (easiest to read obviously)
{'B': 'B', 'D': 'D', 'F': 'F', 'H': 'H', 'J': 'J', 'L': 'L', 'N': 'N', 'P': 'P', 'R': 'R', 'T': 'T', 'V': 'V', 'X': 'X', 'Z': 'Z', 'b': 'b', 'd': 'd', 'f': 'f', 'h': 'h', 'j': 'j', 'l': 'l', 'n': 'n', 'p': 'p', 'r': 'r', 't': 't', 'v': 'v', 'x': 'x', 'z': 'z', 'A': 'A', 'C': 'C', 'E': 'E', 'G': 'G', 'I': 'I', 'K': 'K', 'M': 'M', 'O': 'O', 'Q': 'Q', 'S': 'S', 'U': 'U', 'W': 'W', 'Y': 'Y', 'a': 'a', 'c': 'c', 'e': 'e', 'g': 'g', 'i': 'i', 'k': 'k', 'm': 'm', 'o': 'o', 'q': 'q', 's': 's', 'u': 'u', 'w': 'w', 'y': 'y'}
Their output:
{'A': 'A', 'C': 'C', 'B': 'B', 'E': 'E', 'D': 'D', 'G': 'G', 'F': 'F', 'I': 'I', 'H': 'H', 'K': 'K', 'J': 'J', 'M': 'M', 'L': 'L', 'O': 'O', 'N': 'N', 'Q': 'Q', 'P': 'P', 'S': 'S', 'R': 'R', 'U': 'U', 'T': 'T', 'W': 'W', 'V': 'V', 'Y': 'Y', 'X': 'X', 'Z': 'Z', 'a': 'a', 'c': 'c', 'b': 'b', 'e': 'e', 'd': 'd', 'g': 'g', 'f': 'f', 'i': 'i', 'h': 'h', 'k': 'k', 'j': 'j', 'm': 'm', 'l': 'l', 'o': 'o', 'n': 'n', 'q': 'q', 'p': 'p', 's': 's', 'r': 'r', 'u': 'u', 't': 't', 'w': 'w', 'v': 'v', 'y': 'y', 'x': 'x', 'z': 'z'}
I've already used python to test and compare and they are indeed dicts comprised of the same keys:values. Any advice would be grand. (This is for an online MIT class. There are normally forums, but they are down while there is an exam available. This is NOT for the exam,
Dictionaries in Python don't preserve a particular order. So you can't be sure that the keys and values will be read in the order that they were (a) originally created or (b) last updated.
Quite obviously, the two dictionaries are the same though.
Edit: dict1 == dict2 is sufficient to prove whether two dictionaries are the same; you don't have to check each key/value piecewise.
Just another note, you should be using the modulus % operator instead of - 26.
Replace
if sUpperCase.index(c) + shift > 25:
dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) - 26]
else:
dCode[c] = sUpperCase[(sUpperCase.index(c) + shift)]
With
dCode[c] = sUpperCase[(sUpperCase.index(c) + shift) % 26]