• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# OCW 6.0 ps1b

Adam_M Nov 15th, 2012 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # 6.00 PS1-B Solution
2. # Determines fixed minimum monthly payment needed to finish paying off credit card debt in 1 year
3.
4. # Retrieve input
5. initialBalance = float(raw_input("Enter the outstanding balance on your credit card: "))
6. interestRate = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
7.
8. # Initialize state variables
9. monthlyPayment = 0
10. monthlyInterestRate = interestRate/12
11. balance = initialBalance
12.
13. # Test increasing amounts of monthlyPayment in increments of \$100
14. # until it can be paid off in a year
15. while balance > 0:
16.
17.     monthlyPayment += 10
18.     balance = initialBalance
19.     numMonths = 0
20.
21.     # Simulate passage of time until outstanding balance is paid off
22.     # Each iteration represents 1 month
23.     while numMonths < 12 and balance > 0:
24.
25.         # Count this as a new month
26.         numMonths += 1
27.
28.         # Interest for the month
29.         interest = monthlyInterestRate * balance
30.
31.         # Subtract monthly payment from outstanding balance
32.         balance -= monthlyPayment
33.
34.         # Add interest
35.         balance += interest
36.
37. # Round final balance to 2 decimal places
38. balance = round(balance,2)
39.
40. print "RESULT"
41. print "Monthly payment to pay off debt in 1 year:", monthlyPayment
42. print "Number of months needed:", numMonths
43. print "Balance:",balance
RAW Paste Data
Top