88.3k views
4 votes
Write a python program to find factorial, use exception handling and display an appropriate message if the user inputs alphabets instead of the number. Emulate Index error for a list and handle that exception. Find factorial for all numbers in a given list and display the result.

1 Answer

7 votes

def func(lst):

fac_lst = ([])

try:

for x in lst:

i = 0

fac = 1

while i < x:

fac *= (x - i)

i += 1

fac_lst.append(fac)

return fac_lst

except TypeError:

return "Please input only numbers!"

except IndexError:

return "Please stay within the index!"

lst = ([1, 2, 3, 4, 5, 6, 7, 8])

print(func(lst))

I think this is what you're looking for. Best of luck.

User Ylcnky
by
8.1k points