CMSC 141: Introduction to Python
Due Sunday, July 5, 11:59pm
A loop lets a program repeat work without you writing the same line over
and over. This homework uses both kinds. A for loop runs a set
number of times; a while loop runs until some condition stops
being true. You will also work with lists, which hold many
values in order, and near the end you will call one of your own functions
from inside another.
Everything here was covered in class. Stick to for loops,
range(), lists and .append(), the %
operator, and while loops. You do not need anything we have not
seen yet.
Run git pull to get the assignment, then cd hw2.
All of your work goes in hw2.py, which has six blocks that each
begin with the word def, one per exercise. The values in
parentheses after each name are what you are given to work with; use those
names rather than specific numbers, so your code works for every case the
tests try. Whatever the block should produce goes after the word
return.
Most of this homework is one idea reused. You start a variable at an empty
value, then update it once per loop. To add up numbers you start a total at
0 and add to it. To collect values you start a list at
[] and append to it. Here is the shape for a running total:
total = 0
for i in range(4): # i takes the values 0, 1, 2, 3
total = total + i
# total is now 0 + 1 + 2 + 3 = 6
Notice that range(4) stops before 4, so it gives you
0, 1, 2, 3. That off-by-one is the most common loop mistake, so
keep it in mind.
sum_range(n)Return 0 + 1 + 2 + ... + n using a for loop.
Since range stops just before its argument,
range(n + 1) gives you the values 0 through
n.
>>> sum_range(5)
15
count_evens(numbers)Take a list of whole numbers and return how many of them are even. Loop
over the list and use n % 2 == 0 to test whether a number
n is even. The % operator gives the remainder, so
an even number leaves remainder 0.
>>> count_evens([1, 2, 3, 4, 6])
3
largest(numbers)Return the biggest value in a non-empty list without
using Python's built-in max(). Start by assuming the first item
is the biggest, then loop through and update your guess whenever you find
something larger.
>>> largest([3, 9, 2, 9, 1])
9
squares(n)Return a list of the squares from 0 up to and including
n. This time you are accumulating into a list:
start with [] and use .append() to add each square
as you go.
>>> squares(4)
[0, 1, 4, 9, 16]
percent_even(numbers)Return what percentage of the list is even, rounded to a whole number,
and return 0 for an empty list. This is the first exercise where
you call a function you already wrote: instead of looping again, call your
count_evens(numbers) to get the count, then turn that into a
percentage of len(numbers) and round it with
round(...). Handle the empty list first, since you cannot divide
by zero.
>>> percent_even([1, 2, 3, 4])
50
>>> percent_even([])
0
A for loop runs a known number of times. A while
loop runs as long as a condition holds, which is what you want when you do
not know the count in advance. Python checks the condition before each pass,
so the body has to change something the condition looks at, or the loop never
ends.
months_to_goal(monthly, goal)Suppose you save the same amount every month. Return how many whole months
it takes for your running total to reach or pass the goal. Use a
while loop: keep a running total and a month count, and as long
as the total is below the goal, add one month of savings and add one to the
count. You may assume monthly is a positive number.
>>> months_to_goal(100, 950)
10
>>> months_to_goal(100, 0)
0
With $100 a month and a $950 goal, after 9 months you have saved $900,
which is not enough, so the loop runs once more and the answer is 10. Make
sure the total goes up on every pass; a while loop whose total
never changes will run forever.
We use pytest to check the six functions. From the
hw2 directory, run all the tests with:
uv run pytest
To focus on a single function while you work, add the flags
-xvk followed by its name. The -k keeps only the
tests whose name contains what you type, -x stops at the first
failing case so you can fix one thing at a time, and -v lists
each case as it runs:
uv run pytest -xvk largest
Each case shows up as a pass or a fail, and a failing case prints the value your function returned next to the value the test expected. Fix one function at a time and rerun.
Fill in your answers in hw2.py and answer the short questions
in QUESTIONS-02.txt. Then commit and push your work, and submit
on Gradescope, which pulls your pushed code from GitHub:
git add hw2.py QUESTIONS-02.txt
git commit -m "Complete HW 2"
git push