Thursday, 9 April 2020

Python Program-4

(4) Develop programs to understand working of 
1) Exception Handling and 
2) Assertions.


Program Code:
#Lab-4-Exception Handling
try:
  a = int(input("Enter a:"))
  b = int(input("Enter b:"))
  c = a/b;
  print("a/b = %d"%c)
except Exception:
  print("can't divide by zero")
else:
  print("Hi I am else block")

Output-1:

Output-2:



########################################################################

#The assert Statement
#When it encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true.
   #If the expression is false, Python raises an AssertionError exception.

#!/usr/bin/python

def KelvinToFahrenheit(Temperature):
   assert (Temperature >= 0),  "Colder than absolute zero!"
   return ((Temperature-273)*1.8)+32

print (KelvinToFahrenheit(273))
print (int(KelvinToFahrenheit(505.78)))
print (KelvinToFahrenheit(-5))


Output:

No comments:

Post a Comment