如何在更改变量后重新启动迭代

时间:2015-06-05 15:05:35

标签: python

我正在通过MIT OCW材料学习python和CS。这样做有趣btw因为我认为它很有趣。我正在努力解决一个问题集,即给定的信用卡余额(例如1200美元)和利率(18%),最低月付款需要在12个月或更短时间内完全支付。从100美元开始,如果需要更多或更少,请将其增量更改为10美元。在下面的代码中,如果$ 100的初始启动不起作用,我不知道如何将其增加10,然后再次开始迭代。我甚至不确定从"而#34;是最好的方法。我相信专业人士可以用2行编写代码,但如果有人帮忙,我会很感激,他们会做很长时间,所以像我这样的新手可以遵循它。

balance = float(input("Enter the outstanding balance on your credit card:"))
annual_rate = float(input("Enter the annual credit card interest rate as a decimal:"))

monthly_interest_rate = annual_rate / 12.0
minimum_monthly_payment = 100
updated_balance_each_month = balance * (1 + monthly_interest_rate) - minimum_monthly_payment

month = 1

while month <= 12:
    month += 1
    updated_balance_each_month = updated_balance_each_month * (1 + monthly_interest_rate) - minimum_monthly_payment
    if updated_balance_each_month <= 0:
        print ("Monthly payment to pay off debt in 1 year:", minimum_monthly_payment)
        print ("Number of months needed:",month)
        print (round(updated_balance_each_month,2))
        break
    else:
        minimum_monthly_payment += 10

3 个答案:

答案 0 :(得分:0)

所以在编程中你可以创建函数。这些是您可以从其他地方调用的代码块,它们将执行您放入其中的内容。对于python,语法看起来像......

def function_name(parameter1, parameter2, parameterN):
    #function code here
    #optional return statement
    return parameter1 + parameter2

所以你可以尝试将while循环放在一个函数中,如果while循环成功,则返回true,如果失败则返回false。然后在主体中,如果返回false,则可以选择重做该函数。所以这里有一个如何解决问题的例子。

def try_to_pay_off(payment, interest_rate, start_balance):
    month = 0
    updated_balance = start_balance

    while month <= 12:
        month += 1
        updated_balance = updated_balance * (1 + interest_rate) - payment
        if updated_balance <= 0:
            return True
    # will return false if it goes through 12 months and debt not payed off
    return False

def Main():
    balance = float(input("Enter the outstanding balance on your credit card:"))
    annual_rate = float(input("Enter the annual credit card interest rate as a decimal:"))

    done = False
    monthly_payment = 100
    while not done:
        if try_to_pay_off(monthly_payment,annual_rate,balance):
            done = True
        else:
            monthly_payment += 10
    # after finding the monthly payment required
    print ("Monthly payment to pay off debt in 1 year:", monthly_payment)

通过这种方式,您可以继续使用新值运行您的功能,直到找到可以在一年内支付利息余额的每月付款。

答案 1 :(得分:0)

首先,欢迎使用StackOverflow ,我希望您喜欢编程世界。我听说MIT OCW是一个很好的资源,所以祝你好运,不管你要做什么。

如果我正确理解了这个问题,你必须找到能够偿还债务的最小的最低付款额(在所有12个月内都是相同的)。 (我目前没有评论权限,所以如果我误解了这个问题,如果你澄清了,我会很感激。)

要回答标题中的问题,“重新启动迭代”,您将在第一个之外创建第二个循环。这是一些示例代码。

# given balance and annual_rate

monthly_interest_rate = annual_rate / 12.0
minimum_monthly_payment = 100

# debt_is_paid_off is a use for a variable called a one-way flag
debt_is_paid_off = False

while not debt_is_paid_off:
    updated_balance_each_month = balance
    # range is a neat little python trick to go from 1 to 12 easily
    for month in range(1, 13):
        # update the balance
        updated_balance_each_month *= (1 + monthly_interest_rate) 
        updated_balance_each_month -= minimum_monthly_payment

        # check if it's paid off
        if updated_balance_each_month <= 0:
            debt_is_paid_off = True
            print ("Monthly payment to pay off debt in 1 year:", 
                minimum_monthly_payment)
            print ("Number of months needed:",month)
            print (round(updated_balance_each_month,2))
            # note that this only breaks out of the for-loop.
            # so we need debt_is_paid_off to exit out.
            break
    minimum_monthly_payment += 10

注意代码中有两个循环。外部循环不断增加,直到债务还清时才尝试最低支付,内部循环模拟通过重复偿还债务12次。

然而,标准方法 - 只是使用“break”关键字,就像你所做的那样 - 如果有两个循环,则在Python中不起作用。它只突破了第一个。所以我们使用这个新变量debt_is_paid_off来让我们走出第二个循环。

答案 2 :(得分:0)

好的,谢谢大家的帮助,但我最终想出了一个解决方案(!!!)。我发现在我最初发布的代码中,它将进行第1个月的计算,然后检查updated_balance_each_month是否小于0,如果不是每月最少增加10个月,直到余额等于或低于0而不是运行所有12个月,然后将每月最小值增加10.所以你可以看到我添加了另一个IF语句,以便每月最小值仅在月份== 12且余额高于0时才增加,并且它有效!现在,任何人都可以告诉我如何在这里添加代码,以便它正确显示?我点击{},然后将我的代码粘贴到它所说的位置,但它总是混乱......

balance = float(input("Enter the outstanding balance on your credit card:"))
annual_rate = float(input("Enter the annual credit card interest rate as a decimal:"))

monthly_interest_rate = annual_rate / 12.0
minimum_monthly_payment = 100

updated_balance_each_month = balance * (1 + monthly_interest_rate) - minimum_monthly_payment

month = 1

while month <= 12:

    month += 1

    updated_balance_each_month = updated_balance_each_month * (1 + monthly_interest_rate) - minimum_monthly_payment

    if updated_balance_each_month <= 0:
        print ("Monthly payment to pay off debt in 1 year:", minimum_monthly_payment)
        print ("Number of months needed:",month)
        print (round(updated_balance_each_month,2))
        break
    if month == 12 and updated_balance_each_month > 0:
        minimum_monthly_payment += 10
        month = 0
        updated_balance_each_month = balance