Nov-10-2018, 12:20 AM
(This post was last modified: Nov-10-2018, 12:21 AM by Drone4four.)
(Nov-09-2018, 07:50 PM)nilamo Wrote: The error here is actually the same as the space becomingw
before (though this should also have been a "w", maybe you used a differentshift_variance
this time?). I think the issue boils down to a misunderstanding of whattext[:][:]
does (which is fine, I didn't catch it myself until I actually tried it). Observe:>>> spam = "hello there".split() >>> print(spam) ['hello', 'there'] >>> spam[:] ['hello', 'there'] >>> spam[:][:] ['hello', 'there']
This helps. I am glad this is clarified.
Quote:It's clear from your code that your intention is to have a single list, with each element being a different character, such as['h', 'e', 'l', 'l', 'o', 't', 'h', 'e', 'r', 'e']
. But since "hello" obviously isn't in the original un-shifted alphabet,str.find
is returning-1
, which then means you end up with whatever the last character of the shifted alphabet is, for every word. Your code might work fine if you instead replacetext[:][:]
with"".join(text)
.
Now for the second part. Your function worked fine for encrypting words. It didn't handle spaces correctly, but I'm not sure that's actually an issue.encrypt(a_single_word)
was beautiful. Maybe it's just personal preference, but I would have stopped there, and handled multiple words outside of the encryption function. Maybe something like:def encrypt_phrase(text, shift_variance): words = text.split() encrypted = [] for word in words: encrypted.append(encrypt(word, shift_variance)) return " ".join(encrypted)
Thanks for the advice. I played around with this code.
Quote:And for the third part of this post (I guess I lied when I said there were two parts)...
I don't think you should give up. A cipher isn't the easiest thing in the world to wrap your mind around, so you shouldn't be discouraged that you didn't get it right the first time you tried. Actually, if you embrace failure, and expect that something bad will happen the first couple times you try, you become really good at navigating the builtin help system, or searching online.
Programming is failure, over and over and over again, until it isn't.
I agree that this Caesar cipher project is a bit over my head for where I am currently at. I am now going to shift gears and complete all the remaining Jupyter Notebook practice exercises for these two Python Udemy courses I am taking. Then I'll try writing an online form using Django which prompts the user for a fake credit card number and replaces the middle 8 of the 16 digits with
xxxx xxxx
. At that point I will be ready to return to this Caesar cipher.If I have any further questions or run into any more issues with my code along the way, I will be sure to return to this sub-forum. You ppl are awesome. Special thanks again goes out to @knackwurstbagel, @nilamo, @stullis, and @DeaD_EyE for all your help and advice so far.