如何在循环中使用'if'?

c3n*_*ury 0 python loops

我正试图解决一个问题,但是我觉得我得到的循环在某个地方有一个缺失的链接......

我已经给出了简报 - "现在编写一个程序,计算所需的最低固定月付款,以便在12个月内偿还信用卡余额."

从本质上讲,我所做的远远不是代码,它会取一个基本价值(例如10),从信用卡余额(考虑到利息)中拿走它,如果是它的总月数将余额变为负数(例如已付清)高于12,它会增加'minmonth'(每月还清金额),直到月数等于或低于12.

提前道歉,我实际上只学习了2天的Python!

我哪里错了?

balance = float(raw_input('Enter the outstanding balance on your creditcard: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal:     '))

minmonth = 10
months = 0
monthlyinterest =  interest / 12

while(balance > 0):
    balance = balance * (1 + monthlyinterest) - minmonth
    months = months + 1

    if(months > 12):
        months = 0
        minmonth = minmonth + 10

else:
    print 'RESULT!'
    print 'Total amount to pay per month would be'
    print minmonth
    print 'Total amounts to pay'
    print months
Run Code Online (Sandbox Code Playgroud)

Mak*_*oto 5

缩进在Python中很重要.您可能希望在代码中else使用该if语句.