Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 II. Instructions Write a program that computes the value of 1/n for dif

ID: 3579205 • Letter: P

Question

Python 3

II. Instructions Write a program that computes the value of 1/n for different values of n. To accomplish this, adhere to the following guidelines: Make a function get inverse that accepts a number, n, and then returns the result 1/n. n main, prompt the user for the value, then pass it to your get inverse, and save the result in a variable. Finally, display the result. Create a new exception type: NegativeNumberError that inherits from Exception In the get inverse function you should check for the following rules and raise an appropriate exception. Do not display any error messages in this function, simply raise the exception. n is not a number: ValueError n is 0: ZeroDivisionError

Explanation / Answer

# define Python user-defined exceptions
import sys
sys.tracebacklimit=0
class Error(Exception):
"""Base class for other exceptions"""
pass
class NegetiveNumbererror(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

  
  
def getInverse(s):
print s
  
try:
ret = int(s)
print "ret = ",ret
if ret < 0:
myError = ValueError('a should be a positive')
raise myError
result = 1/float(ret)
return result
except ValueError:
raise NegetiveNumbererror("Error: value must be a number")
except ZeroDivisionError:
raise NegetiveNumbererror("Error: Cant divide by zero")
except NegetiveNumberError:
raise NegetiveNumbererror("Error: Value can not be negetive")
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)
return false

#main
print "Enter a number:",
number = raw_input()
try:
print "Inverse of a number = ",getInverse(number)
except NegetiveNumbererror as e: print str(e)

---------------------------------------------------------

output

Enter a number: KeyboardInterrupt                                                                                                                                               

Enter a number: Inverse of a number =  0                                                                                                                                        

ret =  0                                                                                                                                                                        

'Error: Cant divide by zero'                                                                                                                                                    

Enter a number: Inverse of a number =  -1                                                                                                                                       

ret =  -1                                                                                                                                                                       

'Error: value must be a number'                                                                                                                                                 

Enter a number: Inverse of a number =  kjhk                                                                                                                                     

'Error: value must be a number'                                                                                                                                                 

Enter a number: Inverse of a number =  4                                                                                                                                        

ret =  4                                                                                                                                                                        

0.25