Re: [Tutor] Continue Statement

2013-12-16 Thread spir

On 12/16/2013 03:12 PM, Alina Campana wrote:

Hello dear tutorlist,
I feel terribly ashamed for my bad english...
Yet I'll try to form my question:
It is about the continue statement in python.I wrote this code
i = 0while (i < 10): if i == 5:  continueprint i i+=1
What i expected was an output like12346789
Instead it seems to freeze after 4. The shell input symbol is blinking.

I bet this is easy enough for pros but I really got my problem with 
understanding the reason why.
Okay thank you people, I go now learn better english ^^ 



Your english is as good as mine ;-)

In addition to what others has said:
There are many ways to do what you expected. The simplest one beeing to add a 
variable. In fact, you have to increment i before the test that may lead to 
"continue", else it is never incremented in case i=5; but you also need to keep 
i at its present value for the rest of the present pass in the loop. Example 
solution:


i = 0
while i < 10:
i_for_this_pass = i
i += 1

if i_for_this_pass == 5:
continue
print i_for_this_pass

(Well, the variable name is a bit long, but you see what it means ;-)

Denis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Continue Statement

2013-12-16 Thread Peter Otten
Alina Campana wrote:

> Hello dear tutorlist,

Welcome!

> I feel terribly ashamed for my bad english...
> Yet I'll try to form my question:
> It is about the continue statement in python.I wrote this code
> i = 0while (i < 10):  if i == 5:  continueprint i i+=1
> What i expected was an output like12346789
> Instead it seems to freeze after 4. The shell input symbol is blinking.
> 
> I bet this is easy enough for pros but I really got my problem with
> understanding the reason why. Okay thank you people, I go now learn better
> english ^^

As long as we can understand you at all a few quirks in your English don't 
matter. Quite a few of as aren't native speakers either.

What does matter is how you format your post -- use text-only if possible at 
all. Anyway, I'm guessing that your code originally looked like this:

i = 0
while (i < 10): 
if i == 5:  
continue
print i 
i+=1

Look at the while loop closely -- for 0, 1, 2, 3, and 4 it does not enter 
the if suite. But with i == 5 it executes the continue statement, i. e. it 
goes right back to the while-test. Both `print i` and `i += 1` are skipped, 
i stays at the value 5 forever. 

What you want is to skip the print, but not the increment, and there is no 
straight-forward way to do that using continue and a while loop. But how 
about using for?

for i in range(10):
if i == 5:
continue
print i

In fact I would write it without continue:

for i in range(10):
if i != 5:
print i

Now a variant with while (not recommended), just to show how you could skip 
part of the code using continue:

i = 0
while i < 10:
k = i
i += 1
if k == 5:
continue
print k

But even that would look better without continue:

i = 0
while i < 10:
if i != 5:
print i
i += 1


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Continue Statement

2013-12-16 Thread Mark Lawrence

On 16/12/2013 14:12, Alina Campana wrote:

Hello dear tutorlist,

I feel terribly ashamed for my bad english...


Please *DO NOT* apologise for your English, it's an extremely difficult 
language.




Yet I'll try to form my question:

It is about the continue statement in python.
I wrote this code

i = 0
while (i < 10):
if i == 5:
continue
print i
i+=1

What i expected was an output like
1
2
3
4
6
7
8
9


Exactly what I expected at first glance, but...



Instead it seems to freeze after 4. The shell input symbol is blinking.

I bet this is easy enough for pros but I really got my problem with
understanding the reason why.


You've written an infinite loop.  After 4 is printed i is incremented to 
5.  As i is less than 10 the loop is entered.  The test for i == 5 is 
true so the loop continues, skipping the increment of i.  So i will 
never vary, the loop will keep running and the test for i == 5 is true 
so the loop continues, skipping the increment of i...




Okay thank you people, I go now learn better english ^^



Your English was perfectly understandable.  I suggest you concentrate on 
your code :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Continue Statement

2013-12-16 Thread Alan Gauld

On 16/12/13 14:12, Alina Campana wrote:

Please use plain text for emails. Rich text tends to mess up the 
indentation as you can see below. However via the web interface I was 
able to see what you did so...




i = 0
while (i < 10):
if i == 5:
continue
print i
i+=1

Instead it seems to freeze after 4. The shell input symbol is blinking.


The continue jumps back to the start of the loop. But you have not
at that point incremented i so it is still 5, and remains 5 forever.
If you move the i += 1 line to the top of the loop it will work as you 
expect except you will print 1-10 instead of 0-9.



Okay thank you people, I go now learn better english ^^


Your English is fine, no problem.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Continue Statement

2013-12-16 Thread Alina Campana
Hello dear tutorlist,
I feel terribly ashamed for my bad english...
Yet I'll try to form my question:
It is about the continue statement in python.I wrote this code
i = 0while (i < 10):if i == 5:  continueprint i i+=1
What i expected was an output like12346789
Instead it seems to freeze after 4. The shell input symbol is blinking.

I bet this is easy enough for pros but I really got my problem with 
understanding the reason why.
Okay thank you people, I go now learn better english ^^ 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor