0% found this document useful (0 votes)
35 views

Write A Python Program For Factorial Using Recursion

The document discusses Python programs for calculating factorials recursively and handling exceptions when converting values to floats. It includes examples of using the float(), int(), and a custom safe_float() function to handle conversion errors.

Uploaded by

logu1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Write A Python Program For Factorial Using Recursion

The document discusses Python programs for calculating factorials recursively and handling exceptions when converting values to floats. It includes examples of using the float(), int(), and a custom safe_float() function to handle conversion errors.

Uploaded by

logu1234
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a python program for factorial using recursion >>>def fact(n) SyntaxError: invalid syntax >>> def fact(n):

if n==0 or n==1: return 1 else: return (n* fact(n-1)) >>> fact(5) 120

>>> float(45) 45.0 >>> int(78.9) 78 >>> float(k)

Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> float(k) NameError: name 'k' is not defined >>> float(["k","r"])

Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> float(["k","r"])

TypeError: float() argument must be a string or a number Write a program using exception handling function >>> def safe_float(val): try: exception = float(val) except ValueError: exception = "object type cannot converted into float" except TypeError: exception = "object type cannot converted into float" return exception >>> safe_float(45) 45.0 >>> safe_float("kji") 'Object type cannot converted into float' >>> safe_float(["k","j"]) 'object type cannot converted into float'

You might also like