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

  • Home
  • SEARCH
  • 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 8491421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:25:08+00:00 2026-06-10T22:25:08+00:00

In the python tutorial is an example (copied below), shouldn’t else be indented? I

  • 0

In the python tutorial is an example (copied below), shouldn’t else be indented? I ran the code and it didn’t work but I indented it (else) and it worked. Is, what I am saying right? If the documentation is wrong, then how do I report it as a bug to python doc guys?

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...
  • 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-06-10T22:25:10+00:00Added an answer on June 10, 2026 at 10:25 pm

    Tha example is working and the indented is fine, have a look here:

                                                        # Ident level:
    >>> for n in range(2, 10):                          # 0 
    ...     for x in range(2, n):                       # 1                          
    ...         if n % x == 0:                          # 2
    ...             print n, 'equals', x, '*', n/x      # 3
    ...             break                               # 3
    ...     else:                                       # 1
    ...         # loop fell through without finding a factor                        
    ...         print n, 'is a prime number'            # 2
    

    As you can see, the else relates to the second for by following this rule:

    Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

    In the example, it means that the else will be called if the second for (in the second line) will finish running but will never run the break command – only if n % x == 0 never eval to TRUE.

    If (n % x == 0) at any point the break will be called the second for will stop, n from the first for will grow by 1, (n = n + 1) and the second for will be called again with a new n.

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

Sidebar

Related Questions

What is the python keyword with used for? Example from: http://docs.python.org/tutorial/inputoutput.html >>> with open('/tmp/workfile',
I'm trying to run NeHe's tutorial here using Python 2.7.3, but It's throwing the
I've got zero experience with Python. I have looked around some tutorial materials, but
I know of the few normally mentioned Python tutorial/guides/introductions, but I was wondering if
I know this is a noob question, but I've worked with Python before and
Using this example from http://docs.python.org/tutorial/classes.html#class-objects : class MyClass: A simple example class i =
I tried the following example code from the tutorial that came along wxPython2.8 Docs
Google has a Python tutorial, and they describe boilerplate code as unfortunate and provide
I am going over a Python tutorial the one where the following example is
I'm following the python tutorial at their site and I'm currently at the break

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.