[Tutor] A problem involving exception handling, need suggestions to improve further
Manprit Singh
manpritsinghece at gmail.com
Wed Nov 4 09:42:34 EST 2020
Dear sir ,
In the below written mail , we are only raising an exception . I need to
make a program that can end normally while displaying a message "Wrong
value of order entered" without error if a user provides inappropriate
input in a function call like ser_gen(9, -1).
Regards
Manprit Singh
On Tue, Nov 3, 2020 at 8:14 PM Flynn, Stephen (Life & Pensions) <
Steve.Flynn at capita.com> wrote:
> > ser_gen(9, 4) will print 11106 which is right answer
>
> > ser_gen(9, -1) will print 'Wrong value of order entered"
>
>
> > Need your suggestions to improve
>
> Is the exercise to actually use a try/except block as it's entirely
> superfluous. Why not:
>
> def ser_gen(x, n):
> if n < 1:
> print("Wrong value of order entered")
> raise ValueError
> else:
> l1 = [x]
> for i in range(1, n):
> l1.append(l1[-1]*10 + x)
> print(sum(l1))
>
>
> This email is security checked and subject to the disclaimer on web-page:
> https://www.capita.com/email-disclaimer.aspx
> _______________________________________________
> 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