Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:56:36+00:00 2026-05-27T15:56:36+00:00

For some reason, when the candidate number reaches 21, it realises 21 is divisible

  • 0

For some reason, when the candidate number reaches 21, it realises 21 is divisible by 3, but then won’t break out of the for loop/ By contrast, for example for 25, is notes 25 is divisible by 5 then breaks out.

It seems specifically, when div == 3, it doesn’t break out of the loop. Why is this?

for cand in range (3,100):
    if cand%2 != 0:
        if ((cand ==3) or (cand ==5) or (cand ==7) or (cand ==11) or (cand == 13)):
            print str(cand) + ' is prime.'
        else:
            x = sqrt(cand)
            y= int (x)
            for div in range(3, (y+2)):
                if (cand%div == 0):
                    print div
                    print str(cand) + ' is divisible by' + str(div)
                    div = 10*y
                elif (div == y):
                    print str(cand) + ' is prime.'

Here’s the output of the code:

3 is prime.
5 is prime.
7 is prime.
3
9 is divisible by3
11 is prime.
13 is prime.
3
15 is divisible by3
17 is prime.
19 is prime.
3
21 is divisible by3
21 is prime.
23 is prime.
5
25 is divisible by5
3
27 is divisible by3
27 is prime.
29 is prime.
31 is prime.
3
33 is divisible by3
33 is prime.
5
35 is divisible by5
37 is prime.
3
39 is divisible by3
39 is prime.
41 is prime.
43 is prime.
3
45 is divisible by3
5
45 is divisible by5
45 is prime.
47 is prime.
7
49 is divisible by7
3
51 is divisible by3
51 is prime.
53 is prime.
5
55 is divisible by5
55 is prime.
3
57 is divisible by3
57 is prime.
59 is prime.
61 is prime.
3
63 is divisible by3
7
63 is divisible by7
5
65 is divisible by5
65 is prime.
67 is prime.
3
69 is divisible by3
69 is prime.
71 is prime.
73 is prime.
3
75 is divisible by3
5
75 is divisible by5
75 is prime.
7
77 is divisible by7
77 is prime.
79 is prime.
3
81 is divisible by3
9
81 is divisible by9
83 is prime.
5
85 is divisible by5
85 is prime.
3
87 is divisible by3
87 is prime.
89 is prime.
7
91 is divisible by7
91 is prime.
3
93 is divisible by3
93 is prime.
5
95 is divisible by5
95 is prime.
97 is prime.
3
99 is divisible by3
9
99 is divisible by9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:56:36+00:00Added an answer on May 27, 2026 at 3:56 pm

    A for loop is not a while loop.

    In Python, the only type of for loop is what in other languages is ‘foreach’ – it iterates through a list of elements. In your case, you’re iterating through a range, ie a list of integers from 3 to y+2. It’s the number of elements in the list that determines when the iteration stops, not the value of any one of them. When you change div within one iteration of the loop, that has no effect on the rest of the items.

    You can use break to break out of the loop if a condition is met. Or if you want to keep your logic, you could try using while instead, but then you’ll need to increment the counter manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, Section 1 works but Section 2 does not. When run in
For some reason, my script isn't writing out the text after I remove the
For some reason my javascript won't execute when my submit button is pressed. It's
For some reason, when php mail() is sent to a text number (via email)
For some reason, this works in all major browsers, but not IE (surprise, surprise).
For some reason I'm having trouble getting my program to run a while loop.
For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, when I try to install SQL Server 2008 Express , I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.