1 00:00:00,000 --> 00:00:00,540 2 00:00:00,540 --> 00:00:03,980 ZAMYLA: Let's slither around the Caesar cipher. 3 00:00:03,980 --> 00:00:08,580 Caesar in Python is going to have the same to-dos as the Caesar problem in C. 4 00:00:08,580 --> 00:00:11,850 We're going to get the key from the user, get the plaintext, 5 00:00:11,850 --> 00:00:16,140 encipher that plaintext, and then finally, print that cipher text back 6 00:00:16,140 --> 00:00:17,490 to the user. 7 00:00:17,490 --> 00:00:19,860 The usage for the Caesar problem in Python 8 00:00:19,860 --> 00:00:22,650 is going to be very similar to the usage in C 9 00:00:22,650 --> 00:00:25,800 whereby we pass in the key in the command line 10 00:00:25,800 --> 00:00:29,800 and then prompt the user later on for the plaintext. 11 00:00:29,800 --> 00:00:31,520 So how do we get the key? 12 00:00:31,520 --> 00:00:34,530 Well, passed in the command line, we access that in C 13 00:00:34,530 --> 00:00:36,720 using the variable argv. 14 00:00:36,720 --> 00:00:41,130 And because it's an array, we access that at index 1. 15 00:00:41,130 --> 00:00:42,960 In Python, it's going to be the same. 16 00:00:42,960 --> 00:00:45,960 But in order to access argv, we're going to have 17 00:00:45,960 --> 00:00:50,370 to add in system, or sys, before that. 18 00:00:50,370 --> 00:00:52,800 Then argv is a string. 19 00:00:52,800 --> 00:00:55,950 So we need to cast that to an integer. 20 00:00:55,950 --> 00:00:58,740 In C, we used the atoi function. 21 00:00:58,740 --> 00:01:02,910 And in Python, we're going to add int before that and then the variable 22 00:01:02,910 --> 00:01:03,540 in brackets. 23 00:01:03,540 --> 00:01:07,530 And there you have your key as an integer. 24 00:01:07,530 --> 00:01:10,560 So the next step is to get the plaintext. 25 00:01:10,560 --> 00:01:15,420 In C, we provided you within the CS50 library the get_string function. 26 00:01:15,420 --> 00:01:19,260 Similarly, we also provide you with the get_string function in Python. 27 00:01:19,260 --> 00:01:22,020 You just have to import the CS50 module. 28 00:01:22,020 --> 00:01:26,160 In this problem, we'll want to prompt the user for their plaintext. 29 00:01:26,160 --> 00:01:29,200 And we'll also want to print back their ciphertext. 30 00:01:29,200 --> 00:01:31,210 We'll use the print function for this. 31 00:01:31,210 --> 00:01:33,960 Now, in Python, it automatically prints something 32 00:01:33,960 --> 00:01:36,952 with a new line unless otherwise specified. 33 00:01:36,952 --> 00:01:39,160 So if you want to print something without a new line, 34 00:01:39,160 --> 00:01:43,110 then you'll have to add in a comma after the string that you're printing 35 00:01:43,110 --> 00:01:48,030 and put an n equals with two empty quotation marks. 36 00:01:48,030 --> 00:01:51,570 So now that you have that, let's talk about enciphering. 37 00:01:51,570 --> 00:01:53,760 The algorithm isn't going to change. 38 00:01:53,760 --> 00:01:57,380 For each character in the plaintext string, if it's alphabetic, 39 00:01:57,380 --> 00:02:01,290 then we'll want to shift that character by the key, preserving the case, 40 00:02:01,290 --> 00:02:04,410 whether uppercase or lowercase. 41 00:02:04,410 --> 00:02:09,090 Say I wanted to print out every letter of my name in a new line. 42 00:02:09,090 --> 00:02:13,650 Then if I have a string containing my name, Zamyla, then in a for loop, 43 00:02:13,650 --> 00:02:15,330 I could do as follows-- 44 00:02:15,330 --> 00:02:21,780 for c in name print c, where c is the iterator variable where 45 00:02:21,780 --> 00:02:25,140 each character is stored. 46 00:02:25,140 --> 00:02:27,930 So now that we have that, we need to remember that we only 47 00:02:27,930 --> 00:02:30,000 shift alphabetic characters. 48 00:02:30,000 --> 00:02:32,310 And we want to preserve the case. 49 00:02:32,310 --> 00:02:35,940 In C, we used isalpha, isupper, and islower functions. 50 00:02:35,940 --> 00:02:38,280 And we have those, as well, in Python. 51 00:02:38,280 --> 00:02:42,810 Just make sure to check how to properly use those functions. 52 00:02:42,810 --> 00:02:47,400 Now we come to the Caesar formula, where we add the key to our plaintext letter 53 00:02:47,400 --> 00:02:51,050 and then modulo that result by 26. 54 00:02:51,050 --> 00:02:52,230 Why the modulo? 55 00:02:52,230 --> 00:02:55,260 Well, remember that we need to do an alphabet wraparound. 56 00:02:55,260 --> 00:02:59,190 So if I have the letter Y, for instance, and add the key of 2, 57 00:02:59,190 --> 00:03:02,910 then I want that to wrap around back to the letter A. 58 00:03:02,910 --> 00:03:06,300 But when we calculated this using our ASCII values, then 59 00:03:06,300 --> 00:03:09,300 we actually didn't get the right value. 60 00:03:09,300 --> 00:03:13,850 Rather, we found that we had to use our alphabetical index, referring to Y 61 00:03:13,850 --> 00:03:17,610 as 24 in order for that all to work. 62 00:03:17,610 --> 00:03:22,410 But then once we got there, we had to cast from the alphabetical index 63 00:03:22,410 --> 00:03:27,540 back to our ASCII index in order to print and display the right letter. 64 00:03:27,540 --> 00:03:32,310 So how do we go back and forth between ASCII values and alphabetical indices? 65 00:03:32,310 --> 00:03:37,050 Well, I bet you that in Python, you'll find these two functions quite helpful. 66 00:03:37,050 --> 00:03:39,570 Once you figure that out, you can successfully 67 00:03:39,570 --> 00:03:41,610 slither around your cipher. 68 00:03:41,610 --> 00:03:45,860 My name is Zamyla, and this was Caesar. 69 00:03:45,860 --> 00:03:48,924