Paying the Minimum - python

I seem to be stuck on this question in MIT 6.00SC course. I am very new to programming.
Question: Problem 1
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 raw_input() to ask for the following three floating point numbers:
the outstanding balance on the credit card
annual interest rate
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.
Here is my new code:
outstand_balance = float(raw_input('Enter the outstanding balance on your credit card:'))
ann_interest_rt = float(raw_input('Enter the annual credit card interest rate as a decimal:'))
min_month_rt = float(raw_input('Enter the minimum monthly payment as a decimal:'))
Month = int(1)
for month in range (0,12):
Min_monthly_payment = round((min_month_rt * outstand_balance), 2)
Interest_paid = round((ann_interest_rt/12 * outstand_balance), 2)
Principle_paid = round((Min_monthly_payment - Interest_paid), 2)
outstand_balance = round((outstand_balance - Min_monthly_payment), 2)
Remaining_balance = round((outstand_balance - Min_monthly_payment) * (1 \+(ann_interest_rt/12)), 2)
Month +=1
print "Minmimum monthly payment $" + str(Min_monthly_payment)
print "Principle Paid $" + str(Principle_paid)
print "Remaining Balance $" + str(Remaining_balance)
print "Month: " + str(Month)
It results in this:
Enter the outstanding balance on your credit card:4800
Enter the annual credit card interest rate as a decimal:.2
Enter the minimum monthly payment as a decimal:.02
Minmimum monthly payment $96.0
Principle Paid $16.0
Remaining Balance $4684.8
Month: 2
Minmimum monthly payment $94.08
Principle Paid $15.68
Remaining Balance $4591.1
Month: 3
Minmimum monthly payment $92.2
Principle Paid $15.37
Remaining Balance $4499.28
Month: 4
Minmimum monthly payment $90.35
Principle Paid $15.05
Remaining Balance $4409.3
Month: 5
Minmimum monthly payment $88.55
Principle Paid $14.76
Remaining Balance $4321.11
Month: 6
Minmimum monthly payment $86.78
Principle Paid $14.47
Remaining Balance $4234.68
Month: 7
Minmimum monthly payment $85.04
Principle Paid $14.17
Remaining Balance $4149.99
Month: 8
Minmimum monthly payment $83.34
Principle Paid $13.89
Remaining Balance $4066.99
Month: 9
Minmimum monthly payment $81.67
Principle Paid $13.61
Remaining Balance $3985.66
Month: 10
Minmimum monthly payment $80.04
Principle Paid $13.34
Remaining Balance $3905.94
Month: 11
Minmimum monthly payment $78.44
Principle Paid $13.07
Remaining Balance $3827.82
Month: 12
Minmimum monthly payment $76.87
Principle Paid $12.81
Remaining Balance $3751.27
Month: 13
Here is what they have:
Enter the outstanding balance on your credit card: 4800
Enter the annual credit card interest rate as a decimal: .2
Enter the minimum monthly payment rate as a decimal: .02
Month: 1
Minimum monthly payment: $96.0
Principle paid: $16.0
Remaining balance: $4784.0
Month: 2
Minimum monthly payment: $95.68
Principle paid: $15.95
Remaining balance: $4768.05
Month: 3
Minimum monthly payment: $95.36
Principle paid: $15.89
Remaining balance: $4752.16
Month: 4
Minimum monthly payment: $95.04
Principle paid: $15.84
Remaining balance: $4736.32
Month: 5
Minimum monthly payment: $94.73
Principle paid: $15.79
Remaining balance: $4720.53
Month: 6
Minimum monthly payment: $94.41
Principle paid: $15.73
Remaining balance: $4704.8
Month: 7
Minimum monthly payment: $94.1
Principle paid: $15.69
Remaining balance: $4689.11
Month: 8
Minimum monthly payment: $93.78
Principle paid: $15.63
Remaining balance: $4673.48
Month: 9
Minimum monthly payment: $93.47
Principle paid: $15.58
Remaining balance: $4657.9
Month: 10
Minimum monthly payment: $93.16
Principle paid: $15.53
Remaining balance: $4642.37
Month: 11
Minimum monthly payment: $92.85
Principle paid: $15.48
Remaining balance: $4626.89
Month: 12
Minimum monthly payment: $92.54
Principle paid: $15.43
Remaining balance: $4611.46
RESULT
Total amount paid: $1131.12
Remaining balance: $4611.46
Why is my number not the same?

You need to update the outstanding balance variable in the iteration, because otherwise it will not change.

Related

Working on a Python project for a salesman. Getting "cannot assign to operator" error

I'm working on a project in Python for a salesman. The project takes the pay from hours worked for the week and the percentage from commission of sales per week and adds them together to give the total weekly pay of the salesman. The problem I'm running into is dividing the sales amount with the percent they receive from each sale. The error is "cannot assign to operator"
#Prompt user for the number of hours worked that week
hours_worked = int(input("How many hours did you work this week? \n"))
#Define hourly wage
per_hour = 30
#Multiply the hourly wage and hours worked that week
total_hours_pay = hours_worked * per_hour
#Prompt user for the number of sales made that week
sales_made = int(input("How many sales did you make this week? \n"))
#Amount of each sale
sales = 250
#Determine commission of each sale
25 / sales = commission
#Multiply the commission times the amount of sales made to retrieve amount of total commission pay
total_sales_pay = sales_made * commission
#Add total commission pay from sales with the total hours pay to get the weekly total
total_week_pay = total_sales_pay + total_hours_pay
print(total_week_pay)
You wrote:
25 / sales = commission
But when assigning something to a variable, you need to put the variable at the start of the statement.
Try
commission = 25 / sales
25 / sales = commission this line throws the error. I think it should be comission = 25 / sales. You try to assign the value of comission to the result of 25 / sales which does not work.

How do I update elements in a list, in a for loop

I am trying to update values within a list, each iteration. I am not sure if this is the best approach. I know about string accumulators, int / float accumulators.
The values that I want to update is:
Minimum monthly payment
Principle Paid
and Remaining Balance
I want to update the values with the updated calculations that are done in the for loop.
More details on the problem.
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 raw_input() to ask for the following three floating point numbers:
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.
[4800.0, 0.18, 0.02]
The output is like this:
Month: 1
Minimum payment: 96.0
Principle paid: 24.0
Remaining Balance: 4776.0
Month: 2
Month: 3
store_val_list = list()
outstanding_balance = float(raw_input('Enter the outstanding balance on your credit card: '))
store_val_list.append(outstanding_balance)
annual_interest_rate = float(raw_input('Enter the annual credit card interest rate as a decimal: '))
store_val_list.append(annual_interest_rate)
minimum_monthly_payment_rate = float(raw_input('Enter the minimum monthly payment rate as a decimal: '))
store_val_list.append(minimum_monthly_payment_rate)
print(store_val_list)
def pay_minimum(val_list):
calc_store_list = list()
for month in range(1, 13):
print('Month:', month)
if month == 1:
# 1. Minimum monthly payment (Minimum monthly payment rate X Balance)
min_payment = val_list[2] * val_list[0]
calc_store_list.append(min_payment)
print('Minimum payment:', min_payment)
# 1.5. Interest Paid (Annual interest rate / 12 months X Balance)
interest_paid = val_list[1] / 12 * val_list[0]
calc_store_list.append(interest_paid)
# print('Interest paid:', interest_paid)
# 2. Principle Paid (Minimum monthly payment - Interest Paid)
principle_paid = min_payment - interest_paid
calc_store_list.append(principle_paid)
print('Principle paid:', round(principle_paid))
# 3. Remaining Balance (Balance - Principal paid)
remaining_balance = val_list[0] - principle_paid
calc_store_list.append(remaining_balance)
print('Remaining Balance:', remaining_balance, '\n')
continue
I am using lists because it's easier to calculate the indexes of the list, than single variables.

Mortgage calculator math error

This program runs fine, but the monthly payment it returns is totally off. For a principal amount of $400,000, interest rate of 11%, and a 10-year payment period, it returns the monthly payment of $44000.16. I googled the equation (algorithm?) for mortgage payments and put it in, not sure where I'm going wrong.
import locale
locale.setlocale(locale.LC_ALL, '')
def mortgage(principal, interest, n):
payment = principal*((interest*(1+interest)**n) / ((1+interest)**n-1))
return payment
principal = float(input("What is the amount of the loan you are taking out? $"))
interest = float(input("What is the interest rate? (%) ")) / 100
n = float(input("How many years? ")) * 12
print
print "Your monthly payment would be", locale.currency(mortgage(principal, interest, n))
The problem is in your interest rate used. You request the annual interest rate and never convert to a monthly interest rate.
From https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula:
r - the monthly interest rate, expressed as a decimal, not a
percentage. Since the quoted yearly percentage rate is not a
compounded rate, the monthly percentage rate is simply the yearly
percentage rate divided by 12; dividing the monthly percentage rate by
100 gives r, the monthly rate expressed as a decimal.
I just tried this on my computer and dividing the interest rate by 12 calculated $5510/month which agrees with other mortgage calculators.

How do I call my Python function?

Here is my assignment:
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.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
monthlyPaymentRate - minimum monthly payment rate as a decimal
For each month, calculate statements on the monthly payment and
remaining balance, and print to screen something of the format:
Month: 1
Minimum monthly payment: 96.0
Remaining balance: 4784.0
Finally, print out the total amount paid that year and the remaining
balance at the end of the year in the format:
It should not specify the values for the variables balance,
annualInterestRate, or monthlyPaymentRate - our test code will define
those values before testing your submission.
And here is the code I wrote:
def minpayment(balance, annualInterestRate, monthlyPaymentRate):
totalPaid = 0
month = 1
while month <= 12:
minPayment = monthlyPaymentRate * balance
balance -= minPayment
balance += (annualInterestRate/12.0)*balance
print 'Month:',month
print 'Minimum monthly payment:',round(minPayment,2)
print 'Remaining balance:',round(balance,2)
totalPaid += minPayment
month += 1
print 'Total paid:', round(totalPaid,2)
print 'Remaining balance:', round(balance,2)
Now, my question is, now that I've created the function, how do I call it?
You can call it like:
minpayment(10000, 0.1, 0.3)
or in general:
# You can modify the values below
balance = 10000
annualInterestRate = 0.1
monthlyPaymentRate = 0.3
minpayment(balance, annualInterestRate, monthlyPaymentRate)

What's wrong with this bisection search algorith?

I've got an assignment for my MITx CS class and I am stuck on the following problem:
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
To recap the problem: we are searching for the smallest monthly
payment such that we can pay off the entire balance within a year.
What is a reasonable lower bound for this payment value? $0 is the
obvious anwer, but you can do better than that. If there was no
interest, the debt can be paid off by monthly payments of one-twelfth
of the original balance, so we must pay at least this much every
month. One-twelfth of the original balance is a good lower bound.
What is a good upper bound? Imagine that instead of paying monthly, we
paid off the entire balance at the end of the year. What we ultimately
pay must be greater than what we would've paid in monthly
installments, because the interest was compounded on the balance we
didn't pay off each month. So a good upper bound for the monthly
payment would be one-twelfth of the balance, after having its interest
compounded monthly for an entire year.
In short:
Monthly interest rate = (Annual interest rate) / 12.0 Monthly payment
lower bound = Balance / 12 Monthly payment upper bound = (Balance x (1
+ Monthly interest rate)12) / 12.0
Write a program that uses these bounds and bisection search (for more
info check out the Wikipedia page on bisection search) to find the
smallest monthly payment to the cent (no more multiples of $10) such
that we can pay off the debt within a year. Try it out with large
inputs, and notice how fast it is (try the same large inputs in your
solution to Problem 2 to compare!). Produce the same return value as
you did in Problem 2.
Now, this is what I've been able to come up with but it actually generates a wrong output. I have no idea what's going on wrong with this code:
#------------Defined variables---------------#
balance = 999999
annualInterestRate = 0.18
#------------Defined variables---------------#
monthlyInterestRate = annualInterestRate / 12.0
monthlyPaymentLower = balance / 12
monthlyPaymentUpper = (balance * (1 + monthlyInterestRate)**12) / 12.0
month = 1
total = 0
while (total < balance) and month < 13:
pay = (monthlyPaymentLower + monthlyPaymentUpper) / 2
total += pay
if total < balance:
monthlyPaymentLower = pay
elif total > balance:
monthlyPaymentHigher = pay
month += 1
if month == 13:
total = 0
month = 1
print 'Lowest Payment: ' + str(round(pay, 2))
Help?
Like always, not looking for a complete solution or the source code, just drop some hints on where I've gone wrong. (I always get negative votes. :/ )
In place
total += pay
you have to count balance for 12 mount like in problem 2
for month in range(12):
balance = balance - pay
balance = balance + monthlyInterest
than you have to compare balance with 0 and change monthlyPaymentLower or monthlyPaymentUpper
You have to compare monthlyPaymentLower to monthlyPaymentUpper to see if you can finish
if monthlyPaymentUpper - monthlyPaymentLower < 0.001 :
print "now I can finish searching"
Of course in this situation you will have to change more things in your code :)

Categories