From 3f6f60c956a08a4f04dd223126685ba1c6f2d3d9 Mon Sep 17 00:00:00 2001 From: qtdevops Date: Sat, 26 Jun 2021 12:35:47 +0530 Subject: [PATCH] Added raising exceptions --- June21/StandardLibrary/raisingexceptions.py | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 June21/StandardLibrary/raisingexceptions.py diff --git a/June21/StandardLibrary/raisingexceptions.py b/June21/StandardLibrary/raisingexceptions.py new file mode 100644 index 0000000..2561ca7 --- /dev/null +++ b/June21/StandardLibrary/raisingexceptions.py @@ -0,0 +1,42 @@ +def is_prime(number): + """ + This function calculates if the number is prime or not + + Args: + number(int): Number to check for prime + + Returns: + bool: True if prime False otherwise + + Raises: + ValueError: Value Error is raised when the argument to this function is + not an integer or integer with less than 2 as value + + Examples: + >>> is_prime(7) + True + >>> is_prime(10) + False + """ + if not isinstance(number, int): + raise ValueError('number should be of type int') + if number< 2: + raise ValueError('number should be greater than or equal 2') + for index in range(2, number//2 + 1): + if number%index == 0: + return False + return True + + +if __name__ == '__main__': + while True: + try: + number = int(input('Enter the number: ')) + if is_prime(number): + print(f"{number} is prime") + + choice = input('Do you want to continue: y or n ') + if choice == 'n': + break + except ValueError as ve: + print(ve) \ No newline at end of file