Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
frankdpnw
Group Title
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'
 2 years ago
 2 years ago
frankdpnw Group Title
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'
 2 years ago
 2 years ago

This Question is Closed

Spitfire96 Group TitleBest ResponseYou've already chosen the best response.1
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'.
 2 years ago

frankdpnw Group TitleBest ResponseYou've already chosen the best response.0
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.
 2 years ago

Spitfire96 Group TitleBest ResponseYou've already chosen the best response.1
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?
 2 years ago

shandelman Group TitleBest ResponseYou've already chosen the best response.0
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.
 2 years ago

frankdpnw Group TitleBest ResponseYou've already chosen the best response.0
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.
 2 years ago

shandelman Group TitleBest ResponseYou've already chosen the best response.0
If you're learning your first programming language, know that forelse 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 forelse loop, there's a way one can write it without using a forelse loop.
 2 years ago

Spitfire96 Group TitleBest ResponseYou've already chosen the best response.1
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 forelse loop. And you're welcome @frankdpwn I'm glad I can help.
 2 years ago

bwCA Group TitleBest ResponseYou've already chosen the best response.0
http://docs.python.org/reference/compound_stmts.html#theforstatement 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
 2 years ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.