首页 文章

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

提问于
浏览
-1

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

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)for num in range(10,20):for i in range(2,num):if num%i == 0:j = num / I; print'%d等于%d *%d'%(num,i,j);打破; else:print num,'是素数'

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

1 回答

  • 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')
    

    如果你提到你的语法错误会很好 .

相关问题