Python Bisection Search - python

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))

Related

pylint reports => test.py:32:8: W0104: Statement seems to have no effect (pointless-statement)

This code I wrote for a school assignment. I checked the code with pylint, and pylint reports pointless-statement for all if elif else statements. I don't know how to improve my code, is there someone who could help me out? Thanks
USD = 0.8500
GBP = 1.1700
JPY = 0.0077
RATE = 0.015
FEE_MIN = 2.00
FEE_MAX = 15.00
choise = int(input("Which currency do you want to exchange for euros? "
"1) USD dollar,2) GB pound, 3) JP yen : "))
#USD
if (choise) == 1:
foreign = round(float(input("What amount of dollars do you want to convert"
" to euros? : ")))
euros = round((foreign * USD), 2)
COST = (round((foreign * USD), 2) * RATE)
if COST <= FEE_MIN:
COST = FEE_MIN
elif COST > FEE_MAX:
COST = FEE_MAX
else:
COST
payout = (float(euros) - float(COST))
print("The transactioncost are: {:.2f}.".format(COST))
print("You'll receive {:.2f} euros.".format(payout))
#GBP
if (choise) == 2:
foreign = round(float(input("What amount of pounds do you want to convert"
" to euros? : ")))
euros = round((foreign * GBP), 2)
COST = (round((foreign * GBP), 2) * RATE)
if COST <= FEE_MIN:
COST = FEE_MIN
elif COST > FEE_MAX:
COST = FEE_MAX
else:
COST
payout = (float(euros) - float(COST))
print("The transactioncost are: {:.2f}.".format(COST))
print("You'll receive {:.2f} euros.".format(payout))
#JPY
if (choise) == 3:
foreign = round(float(input("What amount of yen's do you want to convert"
" to euros? : ")))
euros = round((foreign * JPY), 2)
COST = (round((foreign * JPY), 2) * RATE)
if COST <= FEE_MIN:
COST = FEE_MIN
elif COST > FEE_MAX:
COST = FEE_MAX
else:
COST
payout = (float(euros) - float(COST))
print("The transactioncost are: {:.2f}.".format(COST))
print("You'll receive {:.2f} euros.".format(payout))
The problem is here:
else:
COST
Does nothing.

MIT 6.0001 pset1 partC

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

bisection search causes infinite loop

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))

My code does not run in python idle

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))

Using Bisection Search on Lowest Payments on Credit Card debt and

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) )

Categories