Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A for loop launching on Jupyther is not wworking while other lines do

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 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading