[Tutor] For loop with break & else
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Oct 22 04:20:16 EDT 2020
On 22/10/2020 08:21, Manprit Singh wrote:
> Dear sir ,
>
> consider a problem where there is a for loop with break and else:
>
> for i in range(2, y):
> if cond:
> break
> else :
> do_something( )
>
> in this pattern, let 's say if y is a user input. and at a particular time
> the user inputs the value of y equals to 2 . At that time, the "else"
> part will be executed
> or not ?
Why not just ask the interpreter?
>>> def f(y):
i = 0
for i in range(2,y):
if i > 10: break
else: print('in else')
return i
>>> f(2)
in else
0
>>>
Apparently the answer is yes.
--
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