Python Programming Exercises, Gently Explained
40
Solution Template
Try to first write a solution from scratch. But if you have difficulty, you can use the following
partial program as a starting place. Copy the following code from
https://invpy.com/smallest-template.py
and paste it into your code editor. Replace the underscores with code to make a working program:
def getSmallest(numbers):
# If the numbers list is empty, return None:
if len(____) == ____:
return None
# Create a variable that tracks the smallest value so far, and start
# it off a the first value in the list:
smallest = numbers[____]
# Loop over each number in the numbers list:
for number in ____:
# If the number is smaller than the current smallest value, make
# it the new smallest value:
if ____ < smallest:
____ = number
# Return the smallest value found:
____ smallest
The complete solution for this exercise is given in Appendix A and
https://invpy.com/smallest.py.
You can view each step of this program as it runs under a debugger at
https://invpy.com/smallest-debug/.
Dostları ilə paylaş: