Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 作业和问题_Python - Fatal编程技术网

Python 作业和问题

Python 作业和问题,python,Python,下面的代码不起作用,我很沮丧。我知道正确的输出应该是310美元,但不知何故,我的代码没有达到目标。这是edex课程CS和python简介的家庭作业。我试着去评论我认为代码在做什么,但显然我是不对的 任何帮助或提示都将不胜感激 balance = 3329 annualInterestRate = 0.2 monthInterest = annualInterestRate/12.0 # simple payment for the month monthlyPaymentRate = 10

下面的代码不起作用,我很沮丧。我知道正确的输出应该是310美元,但不知何故,我的代码没有达到目标。这是edex课程CS和python简介的家庭作业。我试着去评论我认为代码在做什么,但显然我是不对的

任何帮助或提示都将不胜感激

balance = 3329
annualInterestRate = 0.2
monthInterest = annualInterestRate/12.0

# simple payment for the month
monthlyPaymentRate = 10

# while this is true, run the for loop from 1 - 12. This loop will break if the balance gets <     0, otherwise the 
# monthly payment rate adds by 10 each year. 
while True:
    for month in range(1, 13):
        balance = balance - monthlyPaymentRate
        interestBalance = balance + (monthInterest*balance) 
        balance = interestBalance
        if balance < 0:
            break
        else:
            monthlyPaymentRate += 10

print "balance = " + str(balance)
print "annualInterestRate = " + str(annualInterestRate)

print"Lowest payment: " + str(monthlyPaymentRate)
balance=3329
年利率=0.2
月利率=年利率/12.0
#每月简单付款
月付款率=10
#如果这是真的,那么从1到12运行for循环。如果平衡小于0,此循环将中断,否则
#每月付款率每年增加10%。
尽管如此:
对于范围(1,13)内的月份:
余额=余额-月付款率
利息余额=余额+(剩余月份*余额)
余额=利息余额
如果余额<0:
打破
其他:
月付款率+=10
打印“余额=”+str(余额)
打印“annualInterestate=“+str(annualInterestate))
打印“最低付款:”+str(月付款率)

非常感谢您的评论,我能够调整我的代码以获得适当的结果,通过制作一个函数,意味着我可以在一年内测试每月付款率的结果,返回一个结果,如果结果不是我想要的,我将在while循环中重新运行代码。棘手的业务是学习编码,但相当有趣

任何关于清洁或效率的想法都是最受欢迎的,很确定这不是最有效的步骤

balance = 4400
annualInterestRate = 0.18
monthInterest = annualInterestRate/12.0

# simple payment for the month
monthlyPaymentRate = 0

# while the balance is greater than zero, run the for loop from 1 - 12. This loop will break if 
# the balance gets <=0, otherwise the monthly payment rate adds by 10 each year.
while balance > 0:
    monthlyPaymentRate += 10
    def testBalance(balance, monthlyPaymentRate):
        for month in range(1, 13):
            balance = balance - monthlyPaymentRate
            interestBalance = balance + (monthInterest*balance)
            balance = interestBalance
        return balance

    if testBalance(balance, monthlyPaymentRate) <= 0:
        break

print"Lowest Payment: " + str(monthlyPaymentRate)
balance=4400
年利率=0.18
月利率=年利率/12.0
#每月简单付款
月付款率=0
#当平衡大于零时,从1到12运行for循环。如果
#余额为0:
月付款率+=10
def测试余额(余额,月付款率):
对于范围(1,13)内的月份:
余额=余额-月付款率
利息余额=余额+(剩余月份*余额)
余额=利息余额
返回余额

如果testBalance(balance,monthlyPaymentRate)为我们提供一些关于实际问题的信息。您试图解决的问题是什么?
break
会影响最近的封闭循环(在本例中为
)-当
时,您永远无法逃脱
。更改您的逻辑。您可以为
循环从内部的中断,但在循环期间,您永远不会从外部的中断。将您的条件从
while True
更改为
while balance>=0
,它至少应该运行。为什么您甚至需要一个外部while循环?另外,你能解释一下这个代码应该做什么吗?对不起,这个代码试图计算我在12个月内以20%或0.2的年利率偿还可变余额的最低可能金额。我试着用10美元的初始值测试变量monthlyPaymentRate,如果没有,则增加10美元,直到在12个月内付清余额。打印输出和避免在报价单中添加空格的更为通俗的方法是:
print“lost Payment:”,monthlyPaymentRate
print“最低付款:%s”%monthlyPaymentRate
也在while循环外定义函数或方法testBalance,这会在循环内反复重新定义同一函数,从而浪费资源。顺便说一句,这不是您需要通过编程解决的问题。这是代数(尽管是高级的)。有关详细信息,请参阅