[Tutor] taking support of strings in solving numerical problems

Manprit Singh manpritsinghece at gmail.com
Mon Oct 26 05:23:59 EDT 2020


Dear Sir,

So from the series of mails on this topic, i have learned following facts ,
supported with experiment given below :
>>> def ser_gen(no, order):
           f_no = no
           for i in range(order):
               yield f_no
              f_no = f_no*10 + no


>>> next(ser_gen(9, "A"))                      # execution of generator
function call with an undesired value of second argument ("A") in function
call
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    next(ser_gen(9, "A"))
  File "<pyshell#7>", line 3, in ser_gen
    for i in range(order):
TypeError: 'str' object cannot be interpreted as an integer
>>> next(ser_gen(9, -3))                       # execution of generator
function call with an undesired value of second argument (-3) in function
call
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    next(ser_gen(9, -3))
StopIteration
>>>

1) In the first  execution of the function call, since "A" is string , and
range cannot accept a string, hence Type error is generated .
2) In the second  execution of the function call, since -3 is negative
integer, the for loop never got executed, The function has  returned an
empty iterator, this is proved with the fact that next( ) has caused
StopIteration .

Just need to know if you agree with these two points.

And finally a mechanism is needed in the program to deal with these
unexpected behaviours - like exception handling .

Regards
Manprit Singh




On Mon, Oct 26, 2020 at 1:40 PM Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 26/10/2020 04:55, Cameron Simpson wrote:
>
> >> def ser_gen(no, order):
> >>    f_no = no
> >>    for i in range(order):
> >>        yield f_no
> >>        f_no = f_no*10 + no
> >>
> >> all(ser_gen(9, -3))     Returns True that says, there is an empty
> iterable
> >> inside all( )
> >
> > Returns True for me, too. Which is expected behaviour.
> >
> > It is not clear to me what your objection is about.
>
> To be honest it surprised me too. I expected the function to fall
> off the bottom and return none. But in fact it raises a
> StopIteration instead, which all() interprets as an empty sequence.
>
> --
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list