Hey I'm fairly new to programming and I have a challenge that I just can't figure out entirely. I need to figure out how long it will take for someone to pay off a credit plan with a set interest rate and payment rate. There are two parts of this code I can't seem to figure out. The first is how to stop the while loop when the endingBalance equals 0. Since I can't use endingBalance in the while conditions I'm not sure how to stop it so I just put in a placeholder of 24. The other issue I'm having is how to have the endingBalance stop from going into the negatives. I think I need to make a if statement for the principle to equal the starting balance when the starting balance is less than the principle but at this point I'm not sure how to do that. I just need some help on these two problems and any help would be appreciated.
DOWN_PAYMENT_RATE = 0.1
INTEREST_RATE = 0.12
PAYMENT_RATE = 0.05
balance = price - (price * DOWN_PAYMENT_RATE)
payment = balance * PAYMENT_RATE
print("%0ssssss" % \
("Month", "Starting Balance", "Interest", "Principle", "Payment", "Ending Balance"))
count = 1
while count != 24:
interest = balance * INTEREST_RATE / 12
principle = payment - interest
endingBalance = balance - principle
print("-.2f.2f.2f.2f.2f" % \
(count, balance, interest, principle, payment, endingBalance))
balance = endingBalance
if balance > principle:
principle = principle
else:
principle = balance
count = 1
CodePudding user response:
You just need to update the condition in your while
loop as below:
DOWN_PAYMENT_RATE = 0.1
INTEREST_RATE = 0.12
PAYMENT_RATE = 0.05
balance = price - (price * DOWN_PAYMENT_RATE)
payment = balance * PAYMENT_RATE
print("%0ssssss" % \
("Month", "Starting Balance", "Interest", "Principle", "Payment", "Ending Balance"))
count = 1
endingBalance = balance
while endingBalance > 0:
interest = balance * INTEREST_RATE / 12
principle = payment - interest
endingBalance = balance - principle
if endingBalance <= 0:
break
print("-.2f.2f.2f.2f.2f" % \
(count, balance, interest, principle, payment, endingBalance))
balance = endingBalance
if balance > principle:
principle = principle
else:
principle = balance
count = 1
with the above condition in while
loop the loop will execute only till endingBalance
is >0
and also will never be a negative value
CodePudding user response:
As others have already stated, you have to change your while loop. In addition if you do not want a negative ending balance, you should chance the line principle = payment - interest
.
price=2000
DOWN_PAYMENT_RATE = 0.1
INTEREST_RATE = 0.12
PAYMENT_RATE = 0.05
balance = price - (price * DOWN_PAYMENT_RATE)
payment = balance * PAYMENT_RATE
print("%0ssssss" % \
("Month", "Starting Balance", "Interest", "Principle", "Payment", "Ending Balance"))
count = 1
while balance>0:
interest = balance * INTEREST_RATE / 12
principle = min(payment - interest,balance)
endingBalance = balance - principle
print("-.2f.2f.2f.2f.2f" % \
(count, balance, interest, principle, payment, endingBalance))
balance = endingBalance
if balance > principle:
principle = principle
else:
principle = balance
count = 1
CodePudding user response:
You can declare 'endingBalance' out side while loop with any random ve integer at first because it is not going to effect anything. Because inside while loop its going to change irrespective of its initial value
DOWN_PAYMENT_RATE = 0.1
INTEREST_RATE = 0.12
PAYMENT_RATE = 0.05
balance = price - (price * DOWN_PAYMENT_RATE)
payment = balance * PAYMENT_RATE
print("%0ssssss" % \
("Month", "Starting Balance", "Interest", "Principle", "Payment", "Ending Balance"))
count = 1
endingBalance = 100
while endingBalance > 0:
interest = balance * INTEREST_RATE / 12
principle = payment - interest
endingBalance = balance - principle
print("-.2f.2f.2f.2f.2f" % \
(count, balance, interest, principle, payment, endingBalance))
balance = endingBalance
if balance > principle:
principle = principle
else:
principle = balance
count = 1