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

如何在python中的嵌套循环中运行pdb,python,pdb,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

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

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:对于范围(2,num)中的i:if num%i==0:j=num/i;打印“%d”等于%d*%d%(num,i,j);打破其他:print num“是质数”


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

代码适合我,没有语法错误。可能您正在尝试在Python3中运行Python2代码。试试这个:

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

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

你记得正确缩进吗?你能用错误更新问题吗?(Pdb)范围内的num(10,20):范围内的i(2,num):如果num%i==0:j=num/i;打印“%d”等于%d*%d%(num,i,j);打破else:print num,'is a prime number'***语法错误:无效语法(,第1行)是否尝试从Python shell运行Python脚本?您没有从正确的位置运行代码。如果您在mac上,请使用“终端”应用程序,或在Windows中,按Win+R并打开
cmd
。例如,导航到文件test.py中包含此代码的文件夹。然后运行
python-mpdb test.py
或者简单地运行
python test.py
im在pythonshell中运行它,只有我能够在范围(5):print(“Hello”)中运行这个脚本;印刷品(“世界”);在pdb中打印(i),但可以在上面的代码中执行这正是重点。看看这个:同时也阅读主要答案下面的评论。