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

Python 如何在更改变量后重新启动迭代,python,Python,我正在通过麻省理工学院开放式课程学习python和CS。顺便说一句,这样做是为了好玩,因为我觉得很有趣。我正在尝试解决一个问题,对于给定的信用卡余额(比如1200美元)和利率(18%),最低月付款额是多少,需要在12个月或更短的时间内付清。从100美元开始,如果需要更多或更少,将其增量更改为10美元。在我下面的代码中,如果100美元的初始起始值不起作用,我不知道如何将其增加10,然后再次开始迭代。我甚至不确定从“While”开始是最好的方法。我相信专业人士可以用两行代码来完成这项工作,但如果有人

我正在通过麻省理工学院开放式课程学习python和CS。顺便说一句,这样做是为了好玩,因为我觉得很有趣。我正在尝试解决一个问题,对于给定的信用卡余额(比如1200美元)和利率(18%),最低月付款额是多少,需要在12个月或更短的时间内付清。从100美元开始,如果需要更多或更少,将其增量更改为10美元。在我下面的代码中,如果100美元的初始起始值不起作用,我不知道如何将其增加10,然后再次开始迭代。我甚至不确定从“While”开始是最好的方法。我相信专业人士可以用两行代码来完成这项工作,但如果有人能提供帮助,我将不胜感激,他们做了很长一段路,这样像我这样的新手就可以跟上

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
balance=float(输入(“在您的信用卡上输入未付余额:”)
年利率=浮动(输入(“以小数形式输入信用卡年利率:”)
月利率=年利率/12.0
每月最低付款额=100
每月更新的余额=余额*(1+月利率)-最低月付款
月份=1

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

def function_name(parameter1, parameter2, parameterN):
    #function code here
    #optional return statement
    return parameter1 + parameter2
因此,您可以尝试将while循环放入函数中,如果while循环成功,则返回true,如果失败,则返回false。然后在main中,如果函数返回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)
def try\u to\u pay\u off(付款、利率、起始余额):
月份=0
更新的余额=开始余额

首先,欢迎来到StackOverflow,我希望你喜欢编程的世界。我听说麻省理工开放式课程是一个很好的资源,所以祝你好运,无论你要做什么

如果我理解正确,你必须找到偿还债务的最小最低付款额(在所有十二个月内都是相同的)。(我目前没有发表评论的特权,因此如果我误解了这个问题,如果您能澄清,我将不胜感激。)

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

# 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.0
每月最低付款额=100
#debt_is_Paymed_off_是一个称为单向标志的变量的用法
债务已偿还=错误
虽然未还清债务,但:
每月更新的余额=余额
#范围是一个简单的python小技巧,可以轻松地从1变为12
对于范围(1,13)内的月份:
#更新余额
每月更新的余额*=(1+月利率)
每月更新的余额-=每月最低付款额
#检查是否有回报

如果每个月更新一次余额好的,谢谢大家的帮助,但最后我自己想出了一个解决方案(!!!)。我发现在我最初发布的代码中,它会对第1个月进行计算,然后检查每个月更新的余额是否小于0,如果没有,则在此后的每个月将每月最小值增加10,直到余额等于或低于0,而不是在所有12个月内运行,然后将每月最小值增加10。所以你们可以看到,我添加了另一个IF语句,这样,只有当month==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
balance=float(输入(“在您的信用卡上输入未付余额:”)
年利率=浮动(输入(“以小数形式输入信用卡年利率:”)
月利率=年利率/12.0
每月最低付款额=100
每月更新的余额=余额*(1+月利率)-最低月付款
月份=1

而这个月你可能应该使用两个循环。提示在您的逻辑推理中:“将其增加10,然后再次开始迭代”。如果你必须再次开始迭代,99%的时候这意味着你应该在另一个层次上进行循环。不明白。。。我会在if语句中将更新的_余额设置为零,然后将月份设置为1。。。如果我正确理解你的问题,我也会建议你试着从更高的抽象层次来思考,即定义不同的任务,并将它们放入各自的功能中。例如,检查
每月最低付款额是否足够似乎是一项单独的任务,可以重构为自己的功能。将问题拆分为单独的小问题有助于避免逻辑混乱。一开始可能看起来很复杂,但尽快开始从结构上思考对于避免设计通心粉解决方案的习惯至关重要。我真的很困惑于“未完成时:如果尝试还清(每月付款、年利率、余额):完成=真的”部分。你不需要在函数后面添加一些东西来限定IF语句吗,比如IF函数小于0或者其他什么?所以一般来说,你会放一个操作符来比较值。但是,由于函数已经返回布尔值,它会进行某种自我计算。因为说[if True:]将执行,[if False:]将永远不会执行。因此,您可以使用返回值进行计算。但是你仍然可以把[if try_to_pay_off()==True:]放长一点。谢谢SimKev2!如果你想看到上面的内容,我会给出我自己的解决方案,也许会对我提出批评,并与你的进行比较,也许会说为什么你的比我的好,或者说你的比我的好,或者说这是否有区别。已经非常感谢了,您是一位学者和绅士。在每行代码之前至少应该有4个空格。那是外齿