Python Assert Example - Jquery

How to use assert statement in python?

Snippet Code


  
Rate this page :
  [ 0 votes]

Python assert is a simple statement which allows you to find bugs easily without pain. Here is a function which converts a degree temperature from Kelvin to Fahrenheit.Zero degree kelvin is very cold as it gets the function to be bailed out when there is a negative temperature.

#!/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(420.50)) print KelvinToFahrenheit(-5)

Tags


Ask Questions

Ask Question