Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (anonymous):

I'm on problem 4, building a Caesar Cipher. I successfully finished build_coder and build_encoder. Right now, with build_encoder(shift), it will succesfully return my encoded text with the proper shift, provided I either define plain_text in the function, or outside the function as a global variable. My problem now is how to incorporate apply_coder. From what the directions show, I'm supposed to call the build_encoder function in the variables. (as in apply_coder(text, build_encoder(3)). When I do that, what seems to happen is it tries to run build encoder before even running any of apply_coder. Of course, then it can't find the text it's supposed to encode.

OpenStudy (anonymous):

def build_encoder(shift): plain_text = "Meryl" size = len(plain_text) counter = 0 Combo = build_coder(shift) while counter < size: print Combo[plain_text[counter]], counter += 1

OpenStudy (anonymous):

It didn't print the indents. They're there in the program, and this code will return "Meryl" (or anything else I type into plain_text) as the encoded version.

OpenStudy (anonymous):

build_coder and build_encoder do not return ciphertext - they both return a dictionary that is used to produce the ciphertext. from the docstring of build_(en)coder: """Returns a dict that can apply a Caesar cipher to a letter. ...""" """Returns a dict that can be used to encode a plain text.... """ so the second argument for apply_coder() is a dictionary. apply_coder() applies that dictionary to the text and returns the result. The code you posted for build_encoder() is wrong.

OpenStudy (anonymous):

look at the docstring provided for those functions - they are the function's spec.

OpenStudy (anonymous):

Yep. Looking back it looks like I tried to write the entire program into fewer functions than the assignment says. When I go through the functions, I can see what he's trying to do- each function makes the input needed easier, until the last one only needs you to enter the text and the shift, which is much less complicated for a user.

OpenStudy (anonymous):

i think they were trying to get us used to using functions to abstract code snippits

OpenStudy (anonymous):

Got it working now.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!