Python for循环计数器错误

-3 python counter for-loop

我正在尝试编写一个简短的代码来计算出由于每日兴趣而达到银行中给定本金所需的天数.使用我的代码在IDLE中运行时不会产生任何错误,但计数器返回0.任何想法我错过了什么?

def main():
    # irrelevant code elided by msw, Bal, Int and Tar are numeric
    counter = 0
    for i in range(0):
        if (Bal * Int) == Tar:
            print '1'
        else:
            counter + 1
    print counter
Run Code Online (Sandbox Code Playgroud)

Ned*_*der 6

我不确定你对这个循环有什么看法:

for i in range(0):
    if (Bal * Int) == Tar:
        print '1'
    else:
        counter + 1
Run Code Online (Sandbox Code Playgroud)
  1. range(0) 是一个空列表,所以循环根本不会执行.
  2. counter + 1 只计算一个计数器,它不会增加计数器,你可能意味着 counter += 1
  3. 循环中没有任何东西在每次迭代时都会发生变化,所以如果你进入它,它将是一个无限循环.