1 00:00:00,000 --> 00:00:01,080 2 00:00:01,080 --> 00:00:03,990 ZAMYLA CHAN: Let's get some cash in Python this time. 3 00:00:03,990 --> 00:00:07,290 The logic for the cash problem is remaining the same. 4 00:00:07,290 --> 00:00:08,840 So is the process. 5 00:00:08,840 --> 00:00:11,370 We'll want to prompt the user for an amount of change. 6 00:00:11,370 --> 00:00:15,780 And then using an algorithm that always uses the largest coin possible, 7 00:00:15,780 --> 00:00:17,790 we'll keep track of the amount of coins used 8 00:00:17,790 --> 00:00:21,460 and then print that number back for the user. 9 00:00:21,460 --> 00:00:26,700 So let's compare a couple of things as we translate from C to Python. 10 00:00:26,700 --> 00:00:31,710 As we translate from C to Python, many functions remain the same, for example, 11 00:00:31,710 --> 00:00:32,820 get float. 12 00:00:32,820 --> 00:00:36,810 However, in C, we had to hashtag include the cs50 library, 13 00:00:36,810 --> 00:00:40,200 and in Python we're going to import it. 14 00:00:40,200 --> 00:00:45,270 In terms of validating input, in C we used the do while loop structure. 15 00:00:45,270 --> 00:00:49,230 Now in Python, we don't have a do while loop available, 16 00:00:49,230 --> 00:00:51,490 but we do have a while loop. 17 00:00:51,490 --> 00:00:54,630 So let's see how we can modify a while loop in order 18 00:00:54,630 --> 00:00:57,990 to mimic what the do while loop did. 19 00:00:57,990 --> 00:01:00,180 Consider this snippet of code. 20 00:01:00,180 --> 00:01:05,459 While true I'm going to prompt the user, and then if a certain condition is met 21 00:01:05,459 --> 00:01:07,110 I'm going to break. 22 00:01:07,110 --> 00:01:10,080 This first section ensures that we prompt the user 23 00:01:10,080 --> 00:01:14,620 at least once because the condition for the while loop is true. 24 00:01:14,620 --> 00:01:16,450 Then, we have a condition. 25 00:01:16,450 --> 00:01:19,620 And if that condition is met, executing a break 26 00:01:19,620 --> 00:01:22,690 will break us out of our while loop. 27 00:01:22,690 --> 00:01:25,290 So think about what that condition must be in order 28 00:01:25,290 --> 00:01:29,730 to accept only valid input from the user. 29 00:01:29,730 --> 00:01:32,190 And with that input from the user in dollars, 30 00:01:32,190 --> 00:01:37,680 we yet again want to convert that to cents, using the round function. 31 00:01:37,680 --> 00:01:40,860 Modulo math applies yet again here in Python. 32 00:01:40,860 --> 00:01:44,700 So if you didn't get a chance to explore using the modulo operator 33 00:01:44,700 --> 00:01:47,910 when you implemented this problem way back in C, 34 00:01:47,910 --> 00:01:52,410 then perhaps challenge yourself to use modulo now in Python. 35 00:01:52,410 --> 00:01:55,950 The modulo operator returns the remainder after division. 36 00:01:55,950 --> 00:02:00,570 Take a look at these examples to see how modulo works. 37 00:02:00,570 --> 00:02:05,197 In Python, you'll use two slashes for integer division and one slash 38 00:02:05,197 --> 00:02:06,990 for floating point division. 39 00:02:06,990 --> 00:02:11,190 So try to see if you can use a combination of modulo and division 40 00:02:11,190 --> 00:02:14,010 to implement this cash algorithm. 41 00:02:14,010 --> 00:02:17,550 The very last step is to print the number of coins used. 42 00:02:17,550 --> 00:02:21,420 In C whenever we wanted to print the values of variables, 43 00:02:21,420 --> 00:02:25,860 we had to pass in a place holder to the printf function. 44 00:02:25,860 --> 00:02:29,130 In Python, all we have to do is pass in the variable 45 00:02:29,130 --> 00:02:32,130 itself into the print function. 46 00:02:32,130 --> 00:02:34,480 And there we have completed the problem. 47 00:02:34,480 --> 00:02:36,090 My name is the Zamyla. 48 00:02:36,090 --> 00:02:38,120 And this was cash. 49 00:02:38,120 --> 00:02:39,613