Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Structuring Your Code!
You have completed Structuring Your Code!
Preview
Utilize iterative coding to catch errors and bugs early on.
Iterative coding is a process where you code a small piece of your project and then test it, then code the next piece and test it, so on and so forth.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hi there,
let's chat about iterative coding.
0:00
You may have already been using
this strategy and not even know it.
0:04
[SOUND] Iterative coding always
reminds me of the carpentry mantra,
0:08
measure twice, cut once.
0:13
[SOUND] If you were to tackle
a project without testing any code,
0:15
it would be like cutting wood for
a chair without measuring.
0:19
The chair probably wouldn't turn out
like the way you wanted it to and
0:23
now you have to spend more time
and materials to make it right.
0:28
It's the same for a coding project.
0:33
You would probably end up with bugs in
your code, and trying to find and fix them
0:35
would be more difficult and frustrating
than if you had measured first.
0:40
When writing out your code,
stop every so often and
0:46
test your code by printing
out elements to the console.
0:49
This is your measure twice.
0:52
You'll catch more bugs this way and
end up saving a lot of time and
0:55
headaches in the long run.
0:59
Let me walk you through a small code
challenge to model how this process works.
1:01
In this challenge,
1:07
we need to replace a character in
a word with a different character.
1:08
We're given the word piccolo and
the index is randomly chosen.
1:13
And the character to replace
with is the letter t.
1:20
We need to create the logic inside of
the given function, replace character.
1:24
A good first step is to print out
the random index we are given so
1:30
we can check our work as we go and make
sure we're changing the correct letter.
1:35
Let's also print out that letter
in piccolo at that given index.
1:46
We can do that by using
the index on the string itself.
1:50
Great, don't forget to call
the functions so it will run.
2:00
And lets run the file to see what
gets printed to the console.
2:15
I got 5 and l.
2:32
Since this is randomized,
you may have gotten something different.
2:35
If I run the file again,
I get a different result, nice.
2:39
Count through the word piccolo to see if
you are accessing the right character.
2:46
Numbering always starts at 0 so
with our index of 6,
2:51
Zero, one, two, three, four,
five, six, we get the letter o,
2:58
which is what was printed to the console.
3:03
Now let's get into how to change
this letter into the letter t.
3:08
Strings are immutable.
3:13
So if I try to change
the letter like this,
3:15
When I run the file,
3:29
I get an error.
3:39
I will need to change the string
into something I can change.
3:42
I'm going to break the string into a list,
as it's called in Python or
3:50
an array in other languages.
3:55
I can do this by calling list
with the string as the parameter.
4:05
Let's print out our split string to make
sure it's breaking up the string into
4:12
individual characters like we want.
4:17
Sweet, okay, so now we need to change
the right character into the letter t.
4:36
Luckily, even though the string is now
a list, the index numbers stay the same.
4:44
So let's try what we did earlier but
with our list.
4:51
Again, print out your list to
make sure the changes occurred.
5:07
Great, our changes worked.
5:24
The last thing I need to do is
put the word back together again.
5:26
We can do this by using join.
5:31
The first part is how we want to join,
which is to have no spaces at all.
5:36
Then we call join and
pass it our updated list.
5:42
Then you guessed it,
5:50
let's print out our word to
see it all put together again.
5:52
Nice work.
6:07
Now that we have created the logic,
you can go back and
6:09
remove the other print statements so they
don't take up your console space anymore.
6:12
And you're all done.
6:27
You've completed the challenge.
6:29
This process can seem tedious at times,
but printing out and checking your code
6:31
as you go can help you find errors in your
code a lot faster than writing out all
6:37
of the logic and then working backwards
to figure out where something went wrong.
6:42
It can also help you to better
understand how language works.
6:47
When in doubt, print it out.
6:51
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up