简体   繁体   English

for 循环中的嵌套 if 语句不起作用

[英]nested if statement within a for loop doesn't work

I have a nested if statement within a for loop but the first if statement seems not to be progressing onto the rest of the for loop.我在 for 循环中有一个嵌套的 if 语句,但第一个 if 语句似乎没有进入 for 循环的 rest。

for i, x in enumerate(S[start:-1], start):
    if i > max(sma_period1, sma_period2, sma_period3):
        j = i-start
        
        if ma1[i] == x:
            w[j+1] = w[j]
            cash[j+1] = cash[j]

        if ma1[i] < x: 
            w[j+1] = cash[j]/x  + w[j]
            cash[j+1] = 0

        if ma1[i] > x:
            cash[j+1] = w[j]*x + cash[j]
            w[j+1] = 0

tf_strategy_ma1 = [a*b for a,b in zip(w,S[start:])]+ cash

Sorry if this is a very basic question, I am new to coding and completely stuck.抱歉,如果这是一个非常基本的问题,我是编码新手并且完全卡住了。 Thanks for your help.谢谢你的帮助。

Some debug pointers for a situation like this:针对这种情况的一些调试指针:

  1. you need to get an idea of what the test is seeing.您需要了解测试所看到的内容。
  2. the max() statement as currently show in in your post does not vary each time through the loop and can be calculated and assigned a variable before entering the loop.当前在您的帖子中显示的 max() 语句不会在每次循环中都发生变化,并且可以在进入循环之前计算并分配一个变量。
  3. I would do that, print that value then print the I value on each iteration.我会这样做,打印该值,然后在每次迭代时打印 I 值。 that will give you an idea how the loop is behaving.这将使您了解循环的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM