Home > Software design >  A for loop launching on Jupyter is not working while other lines do
A for loop launching on Jupyter is not working while other lines do

Time:03-15

I am facing the following error. I was working on an interactive session of Jupyter one hour ago. After I turned it on again I have tried to get back to the session and I found out that Jupyter was not able to show the output of a code anymore.

I have tried to restarted Anaconda and the Jupyter session again and the problem persisted just for a for loop, while other parts seem to work fine. Did anyone meet the same problem like this? Can anyone suggest what am I supposed to do?

div = [5,7]
div[0]

for num in range(1,42): 
    if num == 13:
        continue
        if num%div[0]==0:
            print(num, 'the number is divisible for 5')    
            if num%div[1]==0:
                print(num, 'the number is divisible for 7')
                if num%div[0]==0 and num%div[1]==1:
                    print(num, 'the number is divisible for both 5 and 

CodePudding user response:

Your code is much too nested. If a number is not a multiple of 13 nothing at all happens since the program never gets to any of the other if statements and if it is a multiple 13 then it hits the continue statement. In either case, nothing is ever printed. You want something more like:

div = [5,7]
div[0]

for num in range(1,42): 
    if num == 13:
        continue
    if num%div[0]==0:
        print(num, 'the number is divisible for 5')    
    if num%div[1]==0:
        print(num, 'the number is divisible for 7')
    if num%div[0]==0 and num%div[1]==1:
        print(num, 'the number is divisible for both 5 and 7')

This gives output -- buggy output but still output. You have a typo with num%div[1]==1. Since this is evident homework, I'll leave the rest of the debugging as an exercise.

  • Related