Python (Fall 2026) Module 14-12: A14 Explanation Part 2
Watch on YouTube →
Overview
Bill Moseley demonstrates how to refactor Python fruit-management code by moving list-adding and list-removing logic into `addFruit` and `rmFruit` functions that accept and return the modified list. He then recommends practicing by retyping or reconstructing the code, reverse-engineering examples, and checking the assignment requirements in Canvas before submission.
Key takeaways
- Python list operations can be separated from menu code by passing the fruit list into `addFruit` or `rmFruit`, modifying it inside the function, and returning it.
- The demonstrated refactor preserves existing behavior: adding an apple and pear, viewing the list, and removing the pear all work through the extracted functions.
- Retyping example code is more instructive than copying because it builds familiarity with syntax and exposes how each piece of the program is structured.
- Trying to recreate code from notes before consulting the example provides a more demanding way to test understanding of programming logic.
- A working program still needs to meet every requirement listed in Canvas, because those requirements are the basis for grading.
Chapters
- Bill Moseley creates `addFruit` outside the class, passes in the fruit list, appends a new fruit, and returns the modified list.
- He moves the existing four lines of add-fruit code into the function and replaces references to the original `liFruit` with the list argument.
- He applies the same pattern to `rmFruit`, using the passed-in list for removal and returning it afterward.
- A console test adds an apple and a pear, displays the list, removes the pear, and confirms the updated list.
- Moseley suggests keeping the example open as a reference while typing the code into a new document, rather than copying and renaming it.
- A stronger practice exercise is to review notes and the lecture, then try to reproduce the code without looking at the example.
- He compares learning from existing code to typing game programs printed in old computer magazines, which taught both coding patterns and programming syntax.
- Reverse-engineering formatted code can reveal useful implementation techniques, just as artists and musicians adapt ideas from existing work.
- Moseley says to compare the finished program with the requirements listed in Canvas, since those criteria determine grading.
- Testing that the program runs is not enough: students should verify that every listed requirement is satisfied before submitting.
- The assignment models software development work, where a programmer implements a client's explicit list of requested features.
- The next class will revisit object-oriented programming before moving on to creative projects.
Transcript excerpt
0:00 Now, one thing that this doesn't currently do is, it doesn't have functions for adding and removing the fruit. All it does is right here in the middle of these menus, do the adding. So, I'm going to actually show you how I would make the move from just these four lines of code here to doing something using a function. So, I'm going to roll up the class here so I don't have that staring at me. There we go. And right below that, I'm going to create a function. So, def. So, I'm at the same, at the main level of my program, not inside that class. I am creating a function called addFruit. And I'm going to give that the fruit list. I'm going to give it a list as an,
1:02 as a model, and I'm going to return that list after I've modified it. So I want to add a fruit to that list. So how do I do that? Well, here's my main code here, these four lines. So, I'm going to cut those from there, and I'm going to come up here and I'm going to put those four lines of code right inside my function. But instead of liFruit, I'm going to use this internal list. So, it's this list that I pass in. I'm going to append to it and then send it back out. Now,
2:03 there we go. That's why that was giving me a little back talk. All right. So I put that in. I ask the questions, and then I create an instance of the fruit, and then I return it. Is it really that simple? Well, almost. Now, I just need to implement that function. So, what do I do? I'm gonna say addFruit, and I'm going to give it the fruit list as a method, or as a, argument. Sorry.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.