M02 Assignments

Introduction

The lessons for M02 introduce basic logic statements and program flow, such as the if statement.

Assignment Requirements and Examples Below

M02_Assn1 Exercises

M02_Assn2 Exercises

M02_Assn3 Extra Credit

Programming Practices

Some things to consider as you improve your coding skills:

  • Avoid hard coding any literal values in your code: use CONSTANTS instead
  • Define constants at the top of your code section
  • Define constants by naming a global variable in all CAPS (although Python does not have real constants)

Assignments

Requirements Reminder

  • Print a banner line displaying what the program does (with your name)
  • Provide a comment for each group of one or more lines that accomplish similar tasks
  • Be consistent in how you indent code for things like the if statement; always use the same number of tabs or spaces for all idents

M02 Assn1 Exercise #12 (Calculate a Sales Discount)

  • Define the package price as a constant. Make the value a decimal value.
  • Define each discount percentage as a constant
  • Define the number of packages that define each discount as constants

Example:

M03 Exercise #12 Sample Code for Constants
Figure 1: M03 Exercise #12 Sample Code for Constants

Test values: I will use the following values for 3 tests of your program (Use the same values to test your program and confirm you get the correct results!): 55, 22, 15

Output examples:

m02 assn1 exercise #12 test1 example
m02 assn1 exercise #12 test2 example
m02 assn1 exercise #12 test3 example
Figure 2: M02 Exercise #12 Sample Test Outputs

M02 Assn1 Exercise #16 (Determine # of Days in February, by Year)

  • Define the main divisor values (4,100,400) as constants
  • It is NOT necessary to define 0 (zero) - used for comparing values - as a constant (although you can if you wish)
  • Display the where the year entered is a leap year AND the number of days February has for that year

Examples:

Test values: I will use the following values for 3 tests of your program (Use the same values to test your program and confirm you get the correct results!): 2021, 2000, 1968

Output examples:

m02 assn1 exercise 16 test1 example
m02 assn1 exercise 16 test1 example
m02 assn1 exercise 16 test1 example
Figure 3: M02 Exercise #16 Sample Test Outputs

M02 Assn1 Exercise #17 (WiFi Diagnostic Tree)

Programs like this illustrate clearly why constants are a better way to code values that do not change and that may recur multiple times.

  • Define any standard questions, answers and other display text as constants
  • Use a boolean variable (NOT a constant) to keep track of whether the problem was solved at each step
  • The program should stop asking questions if the user responds with "y"
  • Display a final status message (such as "Your problem was solved") only once

Constants Example:

M03 Exercise #17 Sample Code for Constants
Figure 4: M02 Exercise #17 Sample Code for Constants

Test values: I will use the following sequences for testing:
n y
n n n y
n n n n
(Notice the message will be different for the last case)

Output example (last test case only):

m02 assn1 exercise 17 test3 example
Figure 4: M02 Exercise #17 Sample Test Output

M02 Assn 2 Exercise #7 (Calculate Salary in Pennies)

  • Use a for loop or while loop to calculate the salary for each day
  • Display the day number and salary for each day (NOT a running total)
  • Display the total salary for all the days
  • It is not necessary to right-align or otherwise make the display output extra pretty

Test values: I will use the following values for 2 tests of your program (7,15). The totals for each test should be $1.27 and $327.67.

Output examples:

m02 assn2 exercise #7 test1 example
m02 assn2 exercise #7 test2 example
Figure 5: M02 Assn2 Exercise #7 Sample Test Outputs

M02 Assn 2 Exercise #12 (Calculate a Factorial)

  • Use a loop to calculate the factorial
  • Display the running total for each iteration of the loop (a running total)
  • Display the final factorial
  • It is not necessary to right-align or otherwise make the display output extra pretty

Test values: I will make a single test by entering a value of 7. The factorial should be 5,040.

Output example:

m02 assn2 exercise #12 test1 example
Figure 6: M02 Assn2 Exercise #12 Sample Test Outputs

M02 Assn 2 Exercise #14 (Print a Pattern using Nested Loops)

  • There is no user prompt for this program
  • You may use for loops or while loops or one of each
  • Use two loops, one inside the other; one loop controls each line, the other the number of asterisks on that line

Test values: There is no specific test required. The program should simply print the pattern indicated in the exercise.

Output example:

m02 assn2 exercise #14 test1 example
Figure 7: M02 Assn2 Exercise #12 Sample Test Outputs

M02 Assn 3 Extra Credit (Compare Loan Repayment Plans)

The goal of this program is to identify whether a 15-year or 30-year mortgage plan is better, based on the total amount of interest that would be paid.

Write a program to compare how many years it will take to pay off a $250,000 mortgage, depending on the interest rate and payment amount.

Use simple annual interest and the following interest and payment amounts:
- 15-year loan: annual interest rate=2.5% (0.25) and a payment of $1750
- 30-year loan: annual interest rate=3.5% (0.30) and a payment of $1250

In other words, for each year the amount of interest due (i) for each payment is the same: (remaining principal * interest rate) / 12. The amount of principal (p) covered by each monthly payment is also the same for that year (fixed payment amount - interest payment amount). The principal is reduced each month by the amount of principal paid. You can calculate (p) and (i) at the beginning of each year and then calculate the remaining principal after each month.

Calculate the total interest paid for all months and then compare the results for the two repayment plans. Which is better?

The output should show the remaining principal and monthly interest due at the beginning of each year. For debugging purposes, the remaining principal should be shown after each monthly payment is made. But, add a user prompt (for a y or n entry) so the user can decide whether to display the monthly output lines. 

Finally, calculate which repayment plan is better (less interest paid over the course of the loan) and how much is saved for that plan.

If you want something to double-check and estimate how this should work, find an online loan calculator like this one.

  • This program may require 2 nested loops (one for each plan)
  • You can assume the user will enter a valid value (y or n) for the prompt
  • Display the total interest made for each plan
  • Display a recommendation about which plan is better and the amount of interest saved

Test values: There is no specific test required. The program should simply display the data indicated above.

Output example:

m02 assn3 extra credit sample output
Figure 8: M02 Assn3 Extra Credit Sample Outputs