[Tutor] Simple while loop question
Alan Gauld
alan.gauld at yahoo.co.uk
Thu May 11 04:22:09 EDT 2017
On 11/05/17 02:50, Rafael Skovron wrote:
> I dont understand why j can have any value other than zero in this:
>
> for i in range(1, 5):
j = 0
while j < i:
print(j, end = " ")
j += 1
Because i can go as high as 4 (from range(1,5)->1,2,3,4).
So lets consider that final case:
i->4
j->0
j<i -> True
print 0
j->1
j<i->True
print 1
j->2
j<i->True
j->3
print 3
j->4
j<4->False
So you'd expect the output to go like this:
i=1, j->0
i=2, j->0,1
i=3, j->0,1,2
i=4, j->0,1,2,3
And since you set the print end to be a
space it will all run into one line:
0 0 1 0 1 2 0 1 2 3
Is that what you see?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list