如何在python的嵌套循环中运行pdb

时间:2018-12-10 09:58:54

标签: python pdb

我有一个嵌套循环,如下所示,

for num in range(10,20):     #to iterate between 10 to 20
    for i in range(2,num):    #to iterate on the factors of the number
        if num%i == 0:         #to determine the first factor
            j=num/i             #to calculate the second factor
            print '%d equals %d * %d' % (num,i,j)
            break #to move to the next number, the #first FOR
    else:                  # else part of the loop
        print num, 'is a prime number'

当我尝试在pdb中运行它时,

(pdb)对于范围(10,20)中的num:对于范围i中的(2,num):如果num%i == 0:j = num / I;打印'%d等于%d *%d'%(num,i,j);打破; else:打印num,“是素数”

我收到语法错误,我不知道如何在pdb中运行此代码,请建议我如何运行它。

1 个答案:

答案 0 :(得分:0)

该代码对我有用,没有语法错误。可能您正在尝试在python 3中运行python 2代码。尝试以下操作:

for num in range(10,20):     #to iterate between 10 to 20
    for i in range(2,num):    #to iterate on the factors of the number
        if num%i == 0:         #to determine the first factor
            j=num/i             #to calculate the second factor
            print(num,"equals",i*j)
            break #to move to the next number, the #first FOR
    else:                  # else part of the loop
        print(num, 'is a prime number')

,如果您提到语法错误,那就太好了。