0

The second loop doesnt work. When I compile it doesnt output any text, it just asks for the inputs and stops there

It means that the second loop isnt being executed, just the first one but i dont know why

balance0 = float(input("balance = " ))
annualInterestRate = float(input("annualInterestRate = " ))
monthlyPayment = 10
balance = 0


month = 1
while (0):
 balance = balance0
 while month <= 12:
    balance1= (balance + annualInterestRate * balance/12)
    balance1 = balance1 - (monthlyPayment)
    print("Remaining balance month " , month, " is ", balance1)
    balance = balance1
    month += 1
 if balance < 0:
       print("Lowest payment: ", monthlyPayment)
       break

 else: 
     monthlyPayment += 10

The loop

     while month <= 12 

doesnt make it to run, why?

rachuism
  • 193
  • 2
  • 3
  • 9
  • 6
    Because you have the condition `while (0)`, which translates to `while False`, which will never allow you to enter the statement below. Don't you want, instead, `while balance != 0`, or something like that? – blacksite Feb 28 '17 at 00:32
  • `while(0)` is never true and therefore won't be executed. – Cleb Feb 28 '17 at 00:33
  • 3
    looks like both loops dont work if you ask me – RSon1234 Feb 28 '17 at 00:33

2 Answers2

2

It's actually your outer loop that doesn't run. You have:

while (0):

Since (0) is never a true condition, that loop will never execute. Based on the fact that you've got a break in there later on to terminate it when some condition is met, you probably mean:

while (1):

As an aside, while True: is generally equivalent, and probably more idiomatic.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • I feel so stupid right now, I dont know why i remembered it like that haha – rachuism Feb 28 '17 at 00:37
  • @rachuism One good technique to file away for future reference is to temporarily add some trace `print`s at key locations when debugging, so you can quickly visualize the path your program is taking through your code. You can often narrow down the source of problems and quickly discover issues (like the fact that your outer loop was not executing) this way. – Jason C Feb 28 '17 at 00:46
0

Everyone else already caught teh main problem. You also neglected to reset your month counter. The result is that if $10/month doesn't pay off the loan, you go into an infinite loop. Try this:

while (True):
  balance = balance0
  for month in range(1, 12+1):

When you know how many times you need to iterate, use a for statement.

Also, you might use a better customer prompt: annualInterestRate is a variable anem, and hard for a mundane human to read. Change this to "Annual interest rate; e.g. 0.12 would mean 12% interest".

Prune
  • 76,765
  • 14
  • 60
  • 81