Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#19
(Nov-09-2018, 07:50 PM)nilamo Wrote: The error here is actually the same as the space becoming w before (though this should also have been a "w", maybe you used a different shift_variance this time?). I think the issue boils down to a misunderstanding of what text[:][:] 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 replace text[:][:] 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.
Reply


Messages In This Thread
Caesar cipher - by Drone4four - Oct-26-2018, 03:06 PM
RE: Caesar cipher - by nilamo - Oct-26-2018, 03:12 PM
RE: Caesar cipher - by DeaD_EyE - Oct-26-2018, 07:48 PM
RE: Caesar cipher - by Drone4four - Oct-26-2018, 08:47 PM
RE: Caesar cipher - by stullis - Oct-27-2018, 12:20 AM
RE: Caesar cipher - by Drone4four - Oct-28-2018, 12:34 AM
RE: Caesar cipher - by knackwurstbagel - Oct-28-2018, 01:08 AM
RE: Caesar cipher - by stullis - Oct-28-2018, 01:47 AM
RE: Caesar cipher - by nilamo - Oct-28-2018, 03:43 AM
RE: Caesar cipher - by Drone4four - Nov-02-2018, 04:30 PM
RE: Caesar cipher - by nilamo - Nov-02-2018, 05:35 PM
RE: Caesar cipher - by Drone4four - Nov-04-2018, 02:36 AM
RE: Caesar cipher - by stullis - Nov-04-2018, 12:04 PM
RE: Caesar cipher - by nilamo - Nov-04-2018, 09:44 PM
RE: Caesar cipher - by Drone4four - Nov-06-2018, 02:13 AM
RE: Caesar cipher - by stullis - Nov-06-2018, 02:50 AM
RE: Caesar cipher - by Drone4four - Nov-09-2018, 07:22 PM
RE: Caesar cipher - by nilamo - Nov-09-2018, 07:50 PM
RE: Caesar cipher - by Drone4four - Nov-10-2018, 12:20 AM
RE: Caesar cipher - by nilamo - Nov-11-2018, 04:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cipher Caesar Azilkhan 1 2,164 Nov-21-2019, 03:40 PM
Last Post: ichabod801
  No idea how to use the Caesar Cypher in my code celtickodiak 5 3,120 Oct-08-2019, 03:29 AM
Last Post: stullis
  Monoalphabetic cipher pawlo392 1 12,846 Apr-01-2019, 08:51 PM
Last Post: ichabod801
  Vigenere and Caesar Cipher sammy2938 1 5,762 Jul-29-2017, 01:32 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020