[Tutor] A problem involving exception handling, need suggestions to improve further

Flynn, Stephen (Life & Pensions) Steve.Flynn at capita.com
Tue Nov 3 04:21:23 EST 2020


> 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


More information about the Tutor mailing list