Re: [Tutor] SEE THE QUESTION AT THE BOTTOM
Debashish Saha wrote: INPUT: *for n in range(2, 1000):* *for x in range(2, n):* *if n % x == 0:* Please don't add junk characters to your code. There is no need to add asterisks to each line, we can recognise Python code when we see it. Your code cannot run because of the junk added to the start and end of each line. [...] :QUESTION: BUT I COULD NOT UNDERSTAND HOW THE COMMAND ELSE CAN WORK,THOUGH IT IS IN THE OUTSIDE OF THE FOR LOOP IN WHICH IF COMMAND LIES. Please do not SHOUT in ALL CAPITALS, it is considered rude. In Python, "else" is not just for "if" statements: if condition: do_true_condition else: do_false_condition Python also has "else" for for-loops and while-loops: for x in sequence: ... else: # this part runs after the for loop is done while condition: ... else: # this part runs after the while loop is done In both cases, "break" inside the loop will skip past the "else" block without executing it. Try blocks also have an else: try: ... except TypeError: # code that runs if TypeError occurs except ValueError: # code that runs if ValueError occurs else: # code that runs if no error occurs finally: # code that runs no matter what See the tutorial: http://docs.python.org/tutorial/controlflow.html Start here: http://docs.python.org/tutorial/index.html (Although the site seems to be done just at the moment.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SEE THE QUESTION AT THE BOTTOM
On 03-Feb-12 21:38, Debashish Saha wrote: BUT I COULD NOT UNDERSTAND HOW THE COMMAND ELSE CAN WORK,THOUGH IT IS IN THE OUTSIDE OF THE FOR LOOP IN WHICH IF COMMAND LIES. The part that's confusing you is that it is not outside the for loop. It is PART of the for loop syntax. The loop construct used is: for in : else: This means you run , executing once for each iteration, and a statement in that body may elect to break out of the loop prematurely. If nothing breaks out of the loop (i.e., you exit the loop because the was exhausted), then and only then execute . It's a handy mechanism to handle the case where the for loop failed to find whatever it was searching for. Most other languages I've used don't have this, and you end up doing messier manual logic steps to accomplish the same thing. Tip: Please don't type a message IN ALL CAPITAL LETTERS; it gives the impression you are shouting at your audience. HTH, HAND -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] SEE THE QUESTION AT THE BOTTOM
INPUT: *for n in range(2, 1000):* *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'* OUTPUT: 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 :QUESTION: BUT I COULD NOT UNDERSTAND HOW THE COMMAND ELSE CAN WORK,THOUGH IT IS IN THE OUTSIDE OF THE FOR LOOP IN WHICH IF COMMAND LIES. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor