1 00:00:00,002 --> 00:00:05,002 (upbeat music) 2 00:00:05,002 --> 00:00:06,006 - [Instructor] All right, let's examine my code 3 00:00:06,006 --> 00:00:07,006 for this challenge. 4 00:00:07,006 --> 00:00:10,005 We needed to add a new column to the inventory data 5 00:00:10,005 --> 00:00:12,003 to calculate the margin 6 00:00:12,003 --> 00:00:16,000 between the consumer prices and wholesale prices. 7 00:00:16,000 --> 00:00:18,007 So I already have two functions 8 00:00:18,007 --> 00:00:23,004 for reading and writing the CSV data. 9 00:00:23,004 --> 00:00:26,000 So I just needed to write the code 10 00:00:26,000 --> 00:00:28,003 to perform the calculations. 11 00:00:28,003 --> 00:00:33,004 My code starts by reading the Inventory.CSV file, 12 00:00:33,004 --> 00:00:37,000 and then I get the first row of the result, 13 00:00:37,000 --> 00:00:38,007 which is the row of headers. 14 00:00:38,007 --> 00:00:43,005 So I add the new string margin on the end of that list. 15 00:00:43,005 --> 00:00:45,005 That's the easy part, all right? 16 00:00:45,005 --> 00:00:48,003 So then I use an array slice 17 00:00:48,003 --> 00:00:51,008 to get the rows from one until the end of the file. 18 00:00:51,008 --> 00:00:53,001 So this is all the data, right? 19 00:00:53,001 --> 00:00:55,005 This is all the data after the headers. 20 00:00:55,005 --> 00:00:58,002 So for each one of these rows, I loop over 21 00:00:58,002 --> 00:00:59,006 and I calculate the difference 22 00:00:59,006 --> 00:01:03,002 between the values at index four and index three. 23 00:01:03,002 --> 00:01:07,001 And I cast each one of those to a decimal object. 24 00:01:07,001 --> 00:01:09,006 And you can see that if I scroll back up, 25 00:01:09,006 --> 00:01:13,001 I imported the decimal class from the decimal module, 26 00:01:13,001 --> 00:01:15,009 and I'll get to that in a second. 27 00:01:15,009 --> 00:01:18,001 So I calculate the difference 28 00:01:18,001 --> 00:01:21,000 and then I add that value onto the end 29 00:01:21,000 --> 00:01:23,007 of the existing row of the data 30 00:01:23,007 --> 00:01:26,002 and then I just simply write out my array 31 00:01:26,002 --> 00:01:28,007 to the output CSV file. 32 00:01:28,007 --> 00:01:31,008 Now, the reason I need to cast each of these strings 33 00:01:31,008 --> 00:01:33,007 to a decimal value 34 00:01:33,007 --> 00:01:37,001 in order to preserve the floating point precision 35 00:01:37,001 --> 00:01:38,002 of the currency, 36 00:01:38,002 --> 00:01:42,003 and this was the hint that I gave in the previous value, 37 00:01:42,003 --> 00:01:45,001 you could have just cast each of these values 38 00:01:45,001 --> 00:01:47,001 to floating point numbers, 39 00:01:47,001 --> 00:01:50,009 but that actually results in a loss of precision. 40 00:01:50,009 --> 00:01:53,009 So let's see what happens when we do that. 41 00:01:53,009 --> 00:01:59,007 I'm going to change this to float. 42 00:01:59,007 --> 00:02:00,005 All right? 43 00:02:00,005 --> 00:02:05,009 And I'm going to run the code again, all right? 44 00:02:05,009 --> 00:02:08,006 And when I do that, if we look at the output file, 45 00:02:08,006 --> 00:02:11,006 look at the difference, right? 46 00:02:11,006 --> 00:02:13,002 There's all these zeros 47 00:02:13,002 --> 00:02:16,006 and then some teeny tiny little, you know, decimal point 48 00:02:16,006 --> 00:02:17,004 on the end. 49 00:02:17,004 --> 00:02:19,009 You know, in this case, it's, you know, really large. 50 00:02:19,009 --> 00:02:21,004 Some aren't affected that much, 51 00:02:21,004 --> 00:02:25,000 but some, they have all these extra decimals in them, right? 52 00:02:25,000 --> 00:02:27,006 Now, this is not a bug, just to be clear. 53 00:02:27,006 --> 00:02:29,005 This is not a Python bug or anything. 54 00:02:29,005 --> 00:02:34,000 It's a result of how computers store floating point numbers. 55 00:02:34,000 --> 00:02:36,002 So to prevent this from happening, 56 00:02:36,002 --> 00:02:38,007 when we work with currency values, 57 00:02:38,007 --> 00:02:41,006 we use the decimal module, 58 00:02:41,006 --> 00:02:44,006 which I've imported right here at the top, right? 59 00:02:44,006 --> 00:02:48,005 We use the decimal module to preserve currency precision. 60 00:02:48,005 --> 00:02:50,007 And you can learn more about this module, 61 00:02:50,007 --> 00:02:52,003 just go into the Python documentation 62 00:02:52,003 --> 00:02:55,007 and it'll explain how the decimal module works. 63 00:02:55,007 --> 00:02:57,004 All right, so that's my solution. 64 00:02:57,004 --> 00:02:59,000 How does my code compare to yours?