-1

I have a nested loop as shown below,

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'

when I try to run that in pdb like,

(pdb) for num in range(10,20): for i in range(2,num): if num%i == 0: j=num/I; print '%d equals %d * %d' % (num,i,j); break; else: print num, 'is a prime number'

I get syntax error and I don't know how to run this code in pdb, kindly suggest me how to run it.

1 Answers1

0

The code works for me and no syntax error. probably you are trying to run python 2 code in python 3. try this:

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

and would be nice if you mention the syntax error you have.

Yasi Klingler
  • 606
  • 6
  • 13
  • (Pdb) **for num in range(10,20): for i in range(2,num): if num%i == 0:j=num/i;print '%d equals %d * %d' % (num,i,j);break; else:print num, 'is a prime number'** _*** SyntaxError: invalid syntax (, line 1)_ – Sunil Joe Dec 10 '18 at 10:52
  • Are you trying to run a Python script from the Python shell? you are not running the code from the correct place. if you are on mac, use the "terminal" application, or from Windows, press Win+R and open `cmd` . navigate to the folder you have file test.py for example which contains this code. and run `python -m pdb test.py` or simply `python test.py` – Yasi Klingler Dec 10 '18 at 19:36
  • im running it in the python shell only I can able to run this script fine **for i in range(5): print("Hello"); print("World"); print(i)** in pdb but could able to do it in the above code – Sunil Joe Dec 11 '18 at 09:51
  • that's the point exactly. look at this: https://stackoverflow.com/questions/13961140/syntax-error-when-using-command-line-in-python and read also the comments below the main answer. – Yasi Klingler Dec 11 '18 at 22:46