Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes when you run into errors in our code. It's not a syntax error. It's not a runtime error. It's just that the code doesn't quite do what it was that we had intended for it do. These things are often also caused by typos. So let's make a working copy of, incrange-errors.py and we will call that incrange-errors-working.py. This is our inclusive range method from another example in this course.
There is the code, and you will see that it calls it down here, and we will just run it and see what happens. Well, nothing happens. That's not a good sign. So why would nothing happen? Well, what's supposed to happen? The object gets initialized and it gets constructed with these parameters, a start, a stop and a step of 4, 25 and 3, and then it gets printed out by using the object as an iterator, and we can see right here, here is the iterator function.
It's go a yield here, which makes it a generator, so that looks like it should work. We have the start, stop and step, and so here is the start and the stop and the step, and let's just run through the code here and see what happens. I am starting here because this is where the print is, and this is also just the shortest and easiest to look at piece of code. I happen to know, because I wrote this, that this is a little bit more of a hornets nest, and so I am just going to start here because it looks like a simple place to start.
This is where the action is actually happening here. So i equals start, while i is greater than or equal to and that doesn't look right, because the start is at 4, and the stop is at 25, so I would want to run this loop while i <= stop. So that looks like a typo, and so I will change that, and I will save it and run it, and now we are getting some output here.
So let's look at the output and see if it is correct. So we have this range. It starts at 4 and over here we are starting at 3. That's not right. And it stops at 25. Okay, well, that's plausible, and the step is 3, so 3+3 would be 6, 7+3 would be 10. This is actually stepping by 4. It's starting at 3 and stepping by 4. So it would appear that I got those 2 swapped some place.
So let's look at the code and see how this works. So we have numargs, which is the length of args. So that's the number of arguments that I am passing in and if numargs is less than one, we have a TypeError. If numargs equals to one, so I have three arguments. So let's just step down here to numargs equals three and so I assign them like this. Step, stop and start equals args. Well, that's not right. Its start, stop and step is what it should be. So I will switch those. Those got swapped just like I thought. start, stop and step.
And that looks like it should fix it. I will save that and run it, and here we have a different result. Let's see if this looks right. It should start at 4. It is starting at 4. Stop at 25, that looks right, and the step is 3. So we have 4, 7, 10, 13, 16, that looks right. So now it's working as we expected. At this point I would test the other ways of passing parameters, test some other corner cases.
I happen to know because I made these errors that that's the extent of the errors in the file. So the trick is like so much of debugging to be patient, to follow the execution of the code, to look at what's supposed to happen that's not happening, to look at what's happening that's not supposed to happen, and through patience and perseverance to find those little typos, and really the key here is the patience and perseverance. Sometimes this can just take a while.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97456 Viewers
61 Video lessons · 84687 Viewers
71 Video lessons · 68887 Viewers
56 Video lessons · 101331 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.