Você está na página 1de 4

Python Assignment #2: If all else fails.Loop!

When we worked with scratch we had two powerful tools: the repeat block and the if block. We are
going to recreate that functionality with Python and this will really let us go to town with our programs.
Here is a quick sample program to check out:

Lists
Lists work just like in Scratch, we put things in them then we can go and get them by using index
numbers and other useful commands. A few of the most important commands (well pretend we have a
list named myList:
len(myList) This will tell us how many things are in the list.
myList.append(slick) This would add the word slick to the end of the list
myList[start : stop] enter in numbers for start/stop and this will give you a section of your list
These are just a couple of the more useful ones. There is a great list of almost everything you can do on
this site: http://effbot.org/zone/python-list.htm. You can sort, reverse, split and combine listsso many
possibilities!
If/Else statements
These are fairly straightforward, the big thing to remember is that indentation is really important! If you
have indentation then it is inside the if statement and will only trigger if the condition is true. We use
a double equals sign to test for equality and >= or <= to test for greater than or equal to or less than or
equal to. Clever use of the else statement (or combining lots of ifs together) can save you a lot of work!

For Loops
Remember that the goal of using loops is to repeat things that happen over and over again. We want to
automate tasks this way instead of using lots of copy paste! It also makes our lists way more useful!

This assignment will be turned in via email. Please make three separate .py files and attach them to an
email sent to (cbartlo@pps.net). Please put your Full Name and Python Assignment #2 in the subject
line.
Your Tasks:
1) Many Linux-based operating systems have a command (used in the command line) called cal
that will print out a small calendar of the current month (see below). Write a program that will
let the user type in the name of a month and print out an appropriate calendar for 2013. Heres
an example:


Note there is a clever way to do this that doesnt require you to make a list for each separate
months calendar (Hint: Just think of how many days are in each month)!




2) Paying Off Credit Card Debt: Each month, a credit card statement will come with the option for
you to pay a minimum amount of your charge, usually 2% of the balance due. However, the
credit card company earns money by charging interest on the balance that you dont pay. So
even if you pay credit card payments on time, interest is still accruing on the outstanding
balance.
Say youve made a $5,000 purchase on a credit card with 18% annual interest rate and 2%
minimum monthly payment rate. After a year, how much is the remaining balance? Use the
following equations.
Minimum monthly payment = Minimum monthly payment rate x Balance
(Minimum monthly payment gets split into interest paid and principal paid)
Interest Paid = Annual interest rate / 12 months x Balance
Principal paid = Minimum monthly payment Interest paid
Remaining balance = Balance Principal paid
For month 1, we can compute the minimum monthly payment by taking 2% of the balance:
Minimum monthly payment = .02 x $5000.0 = $100.0

We cant simply deduct this from the balance because there is compounding interest. Of this
$100 monthly payment, compute how much will go to paying off interest and how much will go
to paying off the principal. Remember that its the annual interest rate that is given, so we need
to divide it by 12 to get the monthly interest rate.

Interest paid = .18/12.0 x $5000.0 = $75.0
Principal paid = $100.0 $75.0 = $25

The remaining balance at the end of the first month will be the principal paid this month
subtracted from the balance at the start of the month.
Remaining balance = $5000.0 $25.0 = $4975.0

For month 2, we repeat the same steps:
Minimum monthly payment = .02 x $4975.0 = $99.50
Interest Paid = .18/12.0 x $4975.0 = $74.63
Principal Paid = $99.50 $74.63 = $24.87
Remaining Balance = $4975.0 $24.87 = $4950.13
After 12 months, the total amount paid is $1167.55, leaving an outstanding balance of
$4708.10. Pretty depressing!

Write a program to calculate the credit card balance after one year if a person only pays the
minimum monthly payment required by the credit card company each month. Use input() to
ask for the following three floating point numbers (youll have to use float() to convert them):
1. The outstanding balance on the credit card
2. Annual interest rate
3. Minimum monthly payment rate
For each month, print the minimum monthly payment, remaining balance, principle paid in the
format shown in the test cases below. All numbers should be rounded to the nearest penny.
Finally, print the result, which should include the total amount paid that year and the remaining
balance.

3) Fun with Dates: This is a classic mathemagic trick, someone gives you a specific date (i.e.
10/10/05 or January 5
th
, 1952) and then you tell them exactly which day of the week that it took
place. It doesnt require anything too fancy, just a bit of arithmetic. For the sake of simplicity
just assume that you wont have any dates before 1900 entered into your program. There are
many ways to do this. What you are really trying to do is pick a date where you actually know
the day of the week as your anchor (and it has to be kind of far in the past), then you have to
use arithmetic to find out how many days have passed since then.the tricky part is making sure
you count leap years correctly! Then you can use modular arithmetic (since there are only 7
days in the week) to figure out the actual day youve landed on!

a. Before you code, write down your plan of attack. What special conditions do you need
to take into account, how are you going to actually get a day of the week, etc.
b. Come up with a selection of test dates where you actually know which day of the
week the date falls. Make sure you pick a variety of dates to cover all the cases.
c. Write your code!

Você também pode gostar