Winter 2023
For this first assignment, you will write two small Python programs. The following general guidelines and tips apply to both programs.
Refer to lab 1, as well as the lecture slides, to review what you’ve learned so far. In this and future assignments, you may not have seen all the topics in lecture before the assignment is released, but they are available in the textbook and will be covered well before the deadline. As usual, seek help early if you get stuck: come talk to me or the TAs during office hours, or visit the CS tutors for help.
Please keep track of how much time you spend on both portions of this assignment. You will be asked to report your hours on Canvas after you submit. This helps us calibrate the workload for the rest of the quarter, and for future quarters. It’s also useful information about your own study patterns!
The programs you write solution MUST be authored solely by you. You may not view or copy anyone else’s code, or have another person tell you what to type. You can discuss the problems with your peers, but these discussions should happen with Thonny (or your IDE of choice) closed, and you should take a break before returning to write code to ensure that you truly understand your answers. Please see the academic honesty policy on the syllabus for more details, and if you have any questions, or are unsure about whether a specific sort of collaboration violates academic honesty, please come talk to me.
All your programs must have a comment at the top stating the author, date, and a short description of your program. You should also include comments elsewhere in your code anytime you think it’s helpful to outline is happening. Be sure to include a comment when your code does something non-obvious and a reader of your code would benefit from some explanation.
Your code should be written as clearly as possible (i.e., try to avoid the need for explanatory comments). Variable names should follow the guidelines discussed in lecture for sensible naming: neither too verbose nor too terse.
You may assume that the user is well-behaved and enters data as prompted. Your program is not required to check the user’s input to make sure it’s well-formed. Your program is allowed to throw an error if the input is bad (i.e., the user enters a string instead of a number).
Testing is a major component in the process of writing software. Often, testing (detecting errors) and debugging (locating and fixing errors) takes way more effort than writing the code did in the first place. We’ll talk more about testing as the quarter progresses; in the meantime, for each problem below we provide a table with some helpful test cases that you can use to see if your program is working correctly. Try your code out with the given inputs and make sure your output matches the program output specified in the table.
Many online real estate websites have mortgage calculator features1. These calculators ask for some information, such as the price of a home, the down payment (amount of the home price you’d pay up front), and the interest rate, then calculate the amount you’d have to pay monthly on a loan for the home.
According to NerdWallet2, the formula used to calculate the monthly payment based on these inputs is as follows:
\[ M = (P-D) \frac{r (1 + r)^N}{(1+r)^N - 1} \]
Where: \[\begin{aligned} M &= \mbox{The monthly payment}\\ P &= \mbox{The price of the home}\\ D &= \mbox{The down payment amount}\\ N &= \mbox{The number of months over which the loan will be paid off}\\ r &= R*.01/12 \mbox{, the monthly interest rate (the yearly percentage converted to a decimal and divided by 12)}\\ \end{aligned}\]
Write a program called mortgage.py
that prompts the user
to enter (one at a time, and in exactly the given order order) values
for \(P, D, N,\) and \(R\), and outputs the monthly payment amount
\(M\). Notice that you are asked to
prompt the user for \(R\), the annual
interest rate as a percentage (e.g., 3.7), but the formula uses \(r\), the monthly interest rate (e.g.,
0.037).
Here’s a sample invocation of the program:
As you are working, you may notice it’s quite tedious and slow to type in the inputs while you are testing! When you are ready to stop typing all four inputs each time you run your program, you will code the ability to set these numbers using command-line arguments (also called program arguments). If you don’t remember how to use command-line arguments, review Lab 2 where they were covered originally.
When the inputs are given via program arguments, the program runs the same way except it doesn’t prompt the user at all; it goes striaght to outputting the monthly payment amount. A sample run with the same inputs as above supplied as command line arguments is shown here:
To check whether the user has supplied the right number of command
line arguments for this mode, you should use an if/else
statement like the following:
if len(sys.argv) > 1:
# read inputs from command line arguments
else:
# prompt the user for inputs
Recall also that you will need to include import sys
at
the top of your program.
For brevity, the output is truncated after 3 decimal places in the table below; your program will output more decimal places (as in the example invocation).
\(P\) | \(D\) | \(N\) | \(R\) | Output (\(M\)) |
---|---|---|---|---|
100000 | 20000 | 360 | 3.7 | 368.226 |
1000000 | 10 | 180 | 3.4 | 7099.747 |
549050 | 103200 | 800 | 5.1 | 1960.773 |
Congratulations! You’ve just been hired as a Python programmer at an
education start-up company. Your first task is to develop a prototype of
a program that CS students will use to check their understanding of the
modulo operator. The user is quizzed first on num1
modulo
num2
, and then the reverse (num2
modulo
num1
); after answering both questions, the program lets the
user know whether they got each answer right, and prints the correct
answer. Finally, the program
Here’s a screenshot of a sample run of the program:
Pay close attention to the spacing and newlines, and try to get them exactly the same as my program’s. You can customize the message that says whether the user got the correct answer or not.
As above, modify your program to allow inputs to be given as command line arguments or via prompts. When the inputs are given via program arguments, the program runs the same way except it doesn’t prompt the user at all; it goes striaght to the output regarding the answers. A sample run with the same inputs as above supplied as command line arguments is shown here:
First Integer | Second Integer | number 1 % number 2 |
number 2 % number 1 |
---|---|---|---|
7 | 5 | 1 remainder 2 | 0 remainder 5 |
3 | 3 | 1 remainder 0 | 1 remainder 0 |
1 | 678 | 0 remainder 1 | 678 remainder 0 |
8364724 | 9738 | 858 remainder 9520 | 0 remainder 9738 |
Double check that your programs work according to the specification
and produce the output given in the test cases. Take a look through the
rubric below and make sure you won’t lose points for reasons that could
easily be foreseen and fixed. When you’re finished, submit each of your
programs as a .py
file named mortgage.py
and
modhelper.py
. Finally, fill out the A1
Survey on Canvas.
mortgage.py
(15 points)
modhelper.py
(20 points)
num1 % num2
num1 % num2
num2 % num1
num2 % num1
Some assignments will come with an optional challenge problem. In general, these problems will be worth very small amounts of extra credit: this one is worth up to two points. Though the grade payoff is small, you may find them interesting to work on and test your skills in Python and algorithm development. The skills and knowledge needed to solve these problems are not intended to go beyond those needed for the base assignment, but less guidance is provided and more decisions are left up to you. The A1 challenge problem is as follows:
Write a program that takes command line arguments representing the hours and minutes of two times of day, and prints the number of minutes between them. Your program needs to work for time ranges that cross hour boundaries (e.g., 7:59 to 8:01) and day boundaries (e.g., 6:00AM to 5:00AM). You’ll earn 1 point if your program takes the times of day in 24-hour format. For 2 points, the program should handle 12-hour times with AM or PM attached.
Because I haven’t specified the details of how the user specifies the inputs, you should be sure to document this clearly: In addition to the Author, Date, and Description comments at the top of your program, include a "Usage" comment that explains how the program should be called, including at least one example run and the corresponding output.
Upload your challenge program to Canvas in a file called
minutes.py
.
See https://www.zillow.com/mortgage-calculator/ for an example↩︎
Go to https://www.nerdwallet.com/mortgages/mortgage-calculator/calculate-mortgage-payment and click “How to calculate your mortgage payment” for the source of the formula↩︎