First time user of this group. I have a quick question about the python prime number program listed below. In the first loop of ... for x in range (2,n): .... is n = 2 in this case? If yes, why does the program correctly say it is a prime number? I can follow the rest of the logic but and struggling with the first iteration Thanks for n in range (2,15) : for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break else: print n, 'is a prime number'

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

First time user of this group. I have a quick question about the python prime number program listed below. In the first loop of ... for x in range (2,n): .... is n = 2 in this case? If yes, why does the program correctly say it is a prime number? I can follow the rest of the logic but and struggling with the first iteration Thanks for n in range (2,15) : for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break else: print n, 'is a prime number'

MIT 6.00 Intro Computer Science (OCW)
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

Yes that's exactly what it does, it starts at 2 and increases by one every iteration. Basically the start value is always included and the stop value is always excluded in the 'range()' function ( and in many other functions too ) . And if I may make a suggestion, to make the program run faster ( in this case it wouldn't make a difference but it's something to think about for every program you write ) you should make the nested loop only iterate to half of 'n' because anything above that would be equal to a smaller number. Ask if your not sure how to write that. And also you can add user interaction with the program. Maybe you should allow the user to pick the value he/she wishes to iterate from and to using the 'raw_input'.
Thanks .. I am just trying to understand the logic of what was written and I know that it can be improved. Before doing so, I want to make sure I understand the existing logic. However, I am still confused. If n=2, and x=2 then n % x == 0 is true and it should fall thru to the 'print n ='s line and not go to the else statement. I am definitely not grasping this one.
well no it wouldn't because the 'range()' function would exclude the stop value 2, so basically what would happen is that your iteration is starting at 2 and going back 1. Since this can't happen ( if it can someone please correct me ) the inner loop is never entered and it automatically goes to the else statement. That's something I don't understand, that else statement seems to be a branch of the loop ...does someone know whats happening there?

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

There actually is such a thing as a for else loop...if the loop terminates normally without a break, it will do the else clause. If it does break, it will skip the else clause. Spitfire is right: since the range is basically from 2 to 1 when n = 2 that first time, it never even enters the inner for loop and jumps right to the else clause.
Thank you both ... After single stepping through the code multiple times and trying a number of other tests, it can only work the way you describe it and it confirms what I thought but was not sure enough about it.
If you're learning your first programming language, know that for-else loops are, well, kinda weird. They don't appear in a lot of programming languages, and I don't know that I've ever used one. For every for-else loop, there's a way one can write it without using a for-else loop.
Ah that explains why I didn't know about it, the only other language I know is Java and I don't think it has a for-else loop. And you're welcome @frankdpwn I'm glad I can help.
http://docs.python.org/reference/compound_stmts.html#the-for-statement http://docs.python.org/library/functions.html#range the docs installed on your computer are easier to use than the online docs (F1 from IDLE - windows), the index tab is very useful. The tutorial in the docs is a must - http://docs.python.org/tutorial/index.html

Not the answer you are looking for?

Search for more explanations.

Ask your own question