-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.

2
  • Are you remembering to indent properly?
    – Michael Wu
    Dec 10, 2018 at 10:02
  • Can you update question with the error ?
    – Ashish
    Dec 10, 2018 at 10:04

1 Answer 1

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.

4
  • (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 (<stdin>, line 1)
    – Sunil Joe
    Dec 10, 2018 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 Dec 10, 2018 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, 2018 at 9:51
  • that's the point exactly. look at this: stackoverflow.com/questions/13961140/… and read also the comments below the main answer. Dec 11, 2018 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.