1 00:00:00,900 --> 00:00:04,380 Hi there, let's chat about iterative coding. 2 00:00:04,380 --> 00:00:08,978 You may have already been using this strategy and not even know it. 3 00:00:08,978 --> 00:00:13,641 [SOUND] Iterative coding always reminds me of the carpentry mantra, 4 00:00:13,641 --> 00:00:15,530 measure twice, cut once. 5 00:00:15,530 --> 00:00:19,823 [SOUND] If you were to tackle a project without testing any code, 6 00:00:19,823 --> 00:00:23,955 it would be like cutting wood for a chair without measuring. 7 00:00:23,955 --> 00:00:28,291 The chair probably wouldn't turn out like the way you wanted it to and 8 00:00:28,291 --> 00:00:33,110 now you have to spend more time in materials to make it right. 9 00:00:33,110 --> 00:00:35,500 It's the same for a coding project. 10 00:00:35,500 --> 00:00:40,040 You would probably end up with bugs in your code, and trying to find and fix them 11 00:00:40,040 --> 00:00:45,010 would be more difficult and frustrating than if you had measured first. 12 00:00:46,220 --> 00:00:49,440 When writing out your code, stop every so often and 13 00:00:49,440 --> 00:00:52,850 test your code by printing out elements to the console. 14 00:00:52,850 --> 00:00:55,500 This is your measure twice. 15 00:00:55,500 --> 00:00:59,460 You'll catch more bugs this way and end up saving a lot of time and 16 00:00:59,460 --> 00:01:00,720 headaches in the long run. 17 00:01:01,890 --> 00:01:06,110 Let me walk you through a small code challenge to model how this process works. 18 00:01:07,530 --> 00:01:08,540 In this challenge, 19 00:01:08,540 --> 00:01:13,380 we need to replace a character in a word with a different character. 20 00:01:13,380 --> 00:01:19,260 We're given the word piccolo and the index is randomly chosen. 21 00:01:20,490 --> 00:01:23,350 And the character to replace with is the letter t. 22 00:01:24,800 --> 00:01:29,350 We need to create the logic inside of the given function, replace character. 23 00:01:30,990 --> 00:01:35,105 A good first step is to print out the random index we are given so 24 00:01:35,105 --> 00:01:40,343 we can check our work as we go and make sure we're changing the correct letter. 25 00:01:46,112 --> 00:01:50,840 Let's also print out that letter in piccolo at that given index. 26 00:01:50,840 --> 00:01:54,173 We can do that by using the index on the string itself. 27 00:02:00,100 --> 00:02:04,051 Great, don't forget to call the functions so it will run. 28 00:02:15,682 --> 00:02:18,729 And lets run the file to see what gets printed to the console. 29 00:02:32,237 --> 00:02:35,630 I got 5 and l. 30 00:02:35,630 --> 00:02:39,658 Since this is randomized, you may have gotten something different. 31 00:02:39,658 --> 00:02:45,250 If I run the file again, I get a different result, nice. 32 00:02:46,840 --> 00:02:51,360 Count through the word piccolo to see if you are accessing the right character. 33 00:02:51,360 --> 00:02:55,690 Numbering always starts at 0 so with our index of 6, 34 00:02:58,801 --> 00:03:03,614 Zero, one, two, three, four, five, six, we get the letter o, 35 00:03:03,614 --> 00:03:06,576 which is what was printed to the console. 36 00:03:08,143 --> 00:03:12,260 Now let's get into how to change this letter into the letter t. 37 00:03:13,390 --> 00:03:15,700 Strings are immutable. 38 00:03:15,700 --> 00:03:18,576 So if I try to change the letter like this, 39 00:03:29,844 --> 00:03:31,379 When I run the file, 40 00:03:39,499 --> 00:03:40,526 I get an error. 41 00:03:42,955 --> 00:03:47,434 I will need to change the string into something I can change. 42 00:03:50,129 --> 00:03:55,035 I'm going to break the string into a list, as it's called in Python or 43 00:03:55,035 --> 00:03:57,122 an array in other languages. 44 00:04:05,564 --> 00:04:10,983 I can do this by calling list with the string as the parameter. 45 00:04:12,972 --> 00:04:17,679 Let's print out our split string to make sure it's breaking up the string into 46 00:04:17,679 --> 00:04:19,978 individual characters like we want. 47 00:04:36,186 --> 00:04:43,579 Sweet, okay, so now we need to change the right character into the letter t. 48 00:04:44,902 --> 00:04:51,461 Luckily, even though the string is now a list, the index numbers stay the same. 49 00:04:51,461 --> 00:04:54,888 So let's try what we did earlier but with our list. 50 00:05:07,214 --> 00:05:10,449 Again, print out your list to make sure the changes occurred. 51 00:05:24,094 --> 00:05:26,650 Great, our changes worked. 52 00:05:26,650 --> 00:05:31,370 The last thing I need to do is put the word back together again. 53 00:05:31,370 --> 00:05:33,171 We can do this by using join. 54 00:05:36,071 --> 00:05:42,150 The first part is how we want to join, which is to have no spaces at all. 55 00:05:42,150 --> 00:05:47,557 Then we call join and pass it our updated list. 56 00:05:50,778 --> 00:05:52,662 Then you guessed it. 57 00:05:52,662 --> 00:05:56,038 Let's print out our word to see it all put together again. 58 00:06:07,616 --> 00:06:08,427 Nice work. 59 00:06:09,956 --> 00:06:12,868 Now that we have created the logic, you can go back and 60 00:06:12,868 --> 00:06:17,553 remove the other print statements so they don't take up your console space anymore. 61 00:06:27,976 --> 00:06:29,260 And you're all done. 62 00:06:29,260 --> 00:06:30,500 You've completed the challenge. 63 00:06:31,870 --> 00:06:37,040 This process can seem tedious at times, but printing out and checking your code 64 00:06:37,040 --> 00:06:42,020 as you go can help you find errors in your code a lot faster than writing out all 65 00:06:42,020 --> 00:06:47,370 of the logic and then working backwards to figure out where something went wrong. 66 00:06:47,370 --> 00:06:50,640 It can also help you to better understand how language works. 67 00:06:51,700 --> 00:06:53,250 When in doubt, print it out.