Fix the Bug
00:12 I actually need to check whether the location is full before I remove the animals from there, because that’s what’s happening. The animal gets removed from the field before checking whether the location is full. This is why it exits the location that it’s currently in, even if the barn is full. In that case, it’s the order here.
And because both of these conditional statements here have
return statements at the end, so they end the method call if the conditional statement evaluates to
True, I don’t actually need to do these as
So I will also take off the
elif to make that a little clearer and add this space in between. That, for me, visually makes it more clear that we have three individual checks that each mean something different.
The first one checks whether the location you’re trying to move it to is the location that it’s already in. And in that case, no need to run anything else from the method.
return, get out of here.
01:24 Same check is if the new location is full. If the new location is full, all you want to do is tell the user that the new location is full. You don’t want to change anything. That was the issue that we had, right?
01:48 Is it actually somewhere at the moment, or is it in the void? If it’s somewhere, then you want to remove it. And this is independent from the first two checks, which is a little more clear if you have them as separate conditional statements. All right, and then the rest should just stay the way it is.
02:08 Whether or not you removed it from the previous location, you want to assign the new location as the current location, and then you want to append it to the new location and return a little logging message that gives this information. That should be the fix.
02:30 then we get the desired output that the pig is still in the field, and the field now also still contains the pig object. And this was my code running this couple of setup and checks that I added at the bottom of the script to make sure that I can test this.
03:04 It just, again, does it somewhat visually by giving you some output. And it only works because I know what I’m expecting to see here. But this setup code, for example, is an interesting piece that you could carry forward.
03:16 Every time you want to test a method in your code, you’re probably going to need some sort of field, some sort of barn, and some sort of animals. Then maybe you need to fill the barn in a specific test and then move the pig, et cetera.
03:39 But that’s a different story, just a suggestion for you if you want to continue with that. Otherwise, you can also just get rid of it now and be happy that now your farm code is bug free, hopefully.
Here we got the solution for the bug hunt side quest again. This is the updated
.move() method, where you’ve swapped the order of checking whether the new location is full, moving it before you actually remove the animal from the current location.
04:13 And that fixed it. You also cleaned it up a bit by adding white space and just making them separate conditional statements. All right, with this we’re at the end of the course, and in the next lesson I’ll give you a quick recap and then a few more resources that you can use to continue your object-oriented learning journey.
Become a Member to join the conversation.