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

Hands-on - Python - DOCTEST

The document provides a Python implementation for checking if a number is a palindrome and defines a Circle class with methods for calculating area and circumference. It includes doctests to validate the functionality of these methods. Error handling is implemented for invalid inputs in the palindrome function.

Uploaded by

arif895178
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Hands-on - Python - DOCTEST

The document provides a Python implementation for checking if a number is a palindrome and defines a Circle class with methods for calculating area and circumference. It includes doctests to validate the functionality of these methods. Error handling is implemented for invalid inputs in the palindrome function.

Uploaded by

arif895178
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

"""

>>> isPalindrome(121)
True
>>> isPalindrome(344)
False
>>> isPalindrome(-121)
Traceback (most recent call last):
ValueError: x must be positive integer.
>>> isPalindrome("hello")
Traceback (most recent call last):
TypeError: x must be integer.
"""
try:
x = int(x)
temp=x
rev=0
if(x>0):
while(x>0):
dig=x%10
rev=rev*10+dig
x=x//10
if(temp==rev):
return True
else:
return False
elif(x<0):
raise TypeError
else:
raise ValueError
except ValueError:
raise ValueError("x must be positive integer")
except TypeError:
raise TypeError("x must be an integer")

###################################################################################
#######33333

def __init__(self, radius):


# Define the doctests for __init__ method below
"""
>>> c1 = Circle(2.5)
>>> c1.radius
2.5
"""
self.radius = radius

def area(self):
# Define the doctests for area method below
"""
>>> c1 = Circle(2.5)
>>> c1.area()
19.63
"""
# Define the area functionality below
return round(((self.radius ** 2) * math.pi),2)

def circumference(self):
# Define the doctests for circumference method below
"""
>>> c1 = Circle(2.5)
>>> c1.circumference()
15.71
"""
# Define the circumference functionality below
return round((self.radius * 2 * math.pi),2)

You might also like