pset 1 partC
This is what I have now,but it keeps getting into infinite loops. Could someone please tell me what is wrong? Besides, I'm not sure how to deal with the case when it is not possible to save for the down payment in 36 months with a print statement.
semi_annual_raise = 0.07
r = 0.04
portion_down_payment = 0.25
total_cost = 1000000
epsilon = 100
count = 0
low = 0
high = 10000
saving_rate_integer = (low + high) / 2
diff = 1000000
annual_salary = float(input("Please enter your annual salary: "))
monthly_salary = annual_salary / 12
down_payment = portion_down_payment * total_cost
while abs(diff) >= 100:
current_savings = 0
num_month = 0
while num_month <= 36:
if num_month % 6 == 0 and num_month != 0:
monthly_salary = monthly_salary * (1 + semi_annual_raise)
monthly_interest = current_savings * r / 12
current_savings = current_savings + monthly_interest + monthly_salary * (saving_rate_integer/ 10000)
num_month += 1
if current_savings < down_payment:
low = saving_rate_integer
else:
high = saving_rate_integer
saving_rate_integer = (low + high) / 2
count += 1
diff = current_savings - down_payment
saving_rate_float = saving_rate_integer/ 10000
print("Saving rate: ", saving_rate_float)
print("Steps: ", count)
If the starting salary is inadequate, the low rate will keep increasing until it nearly converges on the high rate (i.e. 10000; 100%; the entire salary).
The if/else statement for the savings rate gives a float. If you cast it to an integer (using round), you can force the loop to stop when the savings rate is equal to the upper limit.
while abs(current_savings - down_payment) > epsilon:
step += 1
current_savings = 0
monthly_salary = annual_salary/12
for month in range (0,35):
current_savings += current_savings*(r/12)
current_savings += (monthly_salary*(savings_rate/10000))
month += 1
if month%6 == 0:
monthly_salary *= (1+semi_annual_raise)
if current_savings < down_payment:
low_rate = savings_rate
else:
high_rate = savings_rate
savings_rate = (round(low_rate + high_rate)/2)
if savings_rate == 10000:
break
Related
#This part asks for the user input
annual_salary = float(input("Enter your annual salary:"))
#Declaring all the variables
total_cost = 1000000
High = 1.0
Low = 0.0
portion_saved = (High +Low)/2.0
semi_annual_raise = 0.07
r = 0.04 #annual investment return
portion_down_payment = 0.25
num_guesses = 0
epsilon = 100
total_saving = 0.0
months = 0
#Calculations next
while abs(total_cost - total_saving) >= epsilon:
num_guesses += 1
total_saving = 0.0
while total_saving < (total_cost*portion_down_payment):
monthly_salary = annual_salary/12
monthly_saving = monthly_salary*portion_saved
total_saving += (monthly_saving + total_saving*r/12)
months += 1
if months%6 == 0:
annual_salary += annual_salary*semi_annual_raise
if (total_saving < total_cost):
Low = portion_saved
else:
High = portion_saved
portion_saved = (Low+High)/2.0
print("Best savings rate:", portion_saved)
print("Number of steps:", num_guesses)
But the code enters an infinite loop. When checked using a print command, it turns out that the 'portion_saved' variable takes the value 1.0 on each iteration. But I can't figure out why is this happening. Can someone help?
your code always goes into the if statement, so you are always setting Low to be portion_saved:
1: Low = 0, portion_saved= 1/2
2: Low = 1/2, portion_saved= 1.5/2
3: Low = .75, portion_saved= 1.75/2
4: Low = .875, portion_saved= 1.875/2
5: and so on....
Well, I solved it. Had to make two corrections.
Multiplied the 'total_cost' by 'portions_down_payment' as I really had to calculate the rate to pay the downpayment.
I reset the value of annual sal for each iteration to the initial value.
The final code looks like this:
#This part asks for the user input
starting_salary = float(input("Enter your annual salary:"))
#Declaring all the variables
total_cost = 1000000
High = 1.0
Low = 0.0
portion_saved = (High +Low)/2.0
semi_annual_raise = 0.07
r = 0.04 #annual investment return
portion_down_payment = 0.25
num_guesses = 0
epsilon = 100
total_saving = 0.0
months = 36
print(portion_saved)
#Calculations next
while abs(total_cost*portion_down_payment - total_saving) >= epsilon:
num_guesses += 1
total_saving = 0.0
annual_salary = starting_salary
for i in range (months):
monthly_salary = annual_salary/12
monthly_saving = monthly_salary*portion_saved
total_saving += (monthly_saving + total_saving*r/12)
if i%6 == 0:
annual_salary += annual_salary*semi_annual_raise
if (total_saving > total_cost*portion_down_payment):
High = portion_saved
else:
Low = portion_saved
portion_saved = (Low+High)/2.0
print("Best savings rate:", portion_saved)
print("Number of steps:", num_guesses)
I'm new to Python, currently doing the MIT edX course. We had a problem to complete where we had to use the bisection method to find the lowest payment a person had to make to pay off a credit card dept in a year.We were given the balance and the annual interest rate. My code below works but it does not look correct to me. Does anyone have any insight in this? Thanks
def payment(balance, annualInterestRate):
newBalance = 0
monthlyInterest = annualInterestRate / 12
maxPaybal = (balance * (1 + monthlyInterest) ** 12) / 12
minPaybal = balance/12
while round(newBalance, 2) != 0.01 or round(newBalance, 2) != 0.00:
guess = (minPaybal + maxPaybal) / 2.0
newBalance = balance
months = 12
while months > 0:
prevBalance = newBalance - guess
uptdBalance = prevBalance + (prevBalance * monthlyInterest)
newBalance = uptdBalance
months -= 1
if round(newBalance, 2) == 0.01 or round(newBalance, 2) == 0.00:
return "Lowest Payment: ", round(guess, 2)
elif newBalance < 0.00:
maxPaybal = guess
else:
minPaybal = guess
print(payment(balance, annualInterestRate))
I want to use bisection search to find out how much monthly payment should be in order to pay the full amount of balance within 12 months which user will input. However, this code I write goes into the infinite loop,showing "low, high, montlyPayment infinite times." I don't know which code causes this problem since conditional statement seems right to me .
initialBalance = float(raw_input('Enter the outstanding balance on your credit card'))
annualInterestrate = float(raw_input('Enter the annual credit card interest rate as a decimal'))
monthlyInterestrate = round(annualInterestrate, 2)
balance = initialBalance
while balance > 0:
numMonth = 0
balance = initialBalance
low = balance/12.0
high = (balance*(1+(annualInterestrate/12.0))**12.0)/12.0
epsilon = 0.01
monthlyPayment = round((high + low)/2.0, 2)
while abs(monthlyPayment*12.0 - initialBalance) >= epsilon:
print 'low =', low, 'high =', high, 'monthlyPayment =', round(monthlyPayment,2)
if monthlyPayment*12.0 < balance:
low = monthlyPayment
else:
high = monthlyPayment
monthlyPayment = round((high + low)/2.0, 2)
while balance > 0 and numMonth < 12:
numMonth += 1
interest = monthlyInterestrate * balance
balance -= monthlyPayment
balance += interest
balance = round(balance, 2)
print 'RESULT'
print 'monthly payment to pay off debt in 1 year:', monthlyPayment
print 'Number of months needed:', numMonth
print 'Balance:',balance
I have re-coded the above problem as
balance = 120000
annualInterestRate = 0.1
rate=annualInterestRate/12.0
high=(balance * (1 + rate)**12) / 12.0
low=balance/12.0
payment=0
bal_ref=balance
unpaid=balance
N=0
while (abs(unpaid) > .01):
month=0
pay=(high+low)/2
balance=bal_ref
while(month < 12):
unpaid=balance-pay
balance=unpaid + (unpaid * rate)
month +=1
if (abs(unpaid) < .01):
payment=pay
break
elif (unpaid > .01):
low=pay
elif (unpaid < -.01):
high=pay
N+=1
print("Payment:",round(pay,2))
I am supposed to write a code to pay of credit card debts in an year or less where balance and annual interest rate are the inputs and the output is a monthly installment to pay off the debt in an year or less.The remaining math required for the upper bound and lower bound is in the code.Please help.
balance = int(raw_input())
annualInterestRate = float(raw_input())
lowestPayment = balance
monthlyPayment = 0
monthlyUnpaid = 0
temp = balance
monthlyInterest = 0
lowerBound = balance/12
upperBound = balance*((1+annualInterestRate/12.0)**12)/12.0
mid = 0
while upperBound - lowerBound > 0.0000:
mid = (upperBound + lowerBound)/2.0
monthlyPayment = mid
balance = temp
for i in range(1,13):
monthlyUnpaid = balance - monthlyPayment
monthlyInterest = annualInterestRate/12.0 * monthlyUnpaid
balance = monthlyUnpaid + monthlyInterest
if int(balance) <= 0:
upperBound = mid - 1
if monthlyPayment < lowestPayment:
lowestPayment = monthlyPayment
elif int(balance) > 0:
lowerBound = mid + 1
print 'Lowest Payment: '+str(round(lowestPayment,2))
My code:
monthlyInterestRate = annualInterestRate/12.0
low = balance/12
high = (balance*(1+monthlyInterestRate)**12)/12
guess = (low+high)/2
unpaidBalance = balance
month = 1
while True:
unpaidBalance= unpaidBalance-guess
while month < 13:
if unpaidBalance <= -0.1:
low = guess
month += 1
elif unpaidBalance >= 0.1:
high = guess
month += 1
else:
break
guess = (low + high)/2
print "Lowest Payment: " + str(round(guess, 2))
When I test it it gets stuck at the line "while month < 13:"
Why does it do this and how do I fix it?
If you break at each loop of inner while, you remains less than 13.
And this goes on and on since you proceed While True and do not update your guess.
I fear you are facing infinite looping there.
Your break statement breaks the closest loop, that is the While month < 13 loop. The next line is not read. guessis not updated. The while True is not broken.
Maybe you wanted to say
while month < 13:
unpaidBalance= unpaidBalance-guess
if unpaidBalance <= -0.1:
low = guess
elif unpaidBalance >= 0.1:
high = guess
month += 1
guess = (low + high)/2
here you are
No is the best solution, but it works
monthlyPaymentRate = (balance*annualInterestRate/12)/((1-(1+annualInterestRate/12)**-12))
interest = monthlyPaymentRate * (annualInterestRate/12)
#print (monthlyPaymentRate)
#print (interest)
monthlyPaymentRate = (monthlyPaymentRate - interest) +1
#print (monthlyPaymentRate)
balanceInit = balance
epsilon = 0.01
low = monthlyPaymentRate
while low*12 - balance > epsilon:
balances = balanceInit
for i in range(12):
minpay = monthlyPaymentRate
unpaybal = balances - minpay
interest = (annualInterestRate /12) * unpaybal
smontfinal = unpaybal + interest
balances = smontfinal
#print('Remaining balance: ' ,round(balances,2) )
if balances <0:
low = -1
break
if balances < 0 :
low = -1
else:
monthlyPaymentRate =monthlyPaymentRate + 0.001
print('Lowest Payment:' ,round(monthlyPaymentRate,2) )