Mega Code Archive

 
Categories / Python Tutorial / Data Type
 

Demonstrates handling exceptions when converting string to float

try:     num = float(raw_input("Enter a number: ")) except:     print "Something went wrong!" try:     num = float(raw_input("\nEnter a number: ")) except(ValueError):     print "That was not a number!" try:     num = float(raw_input("\nEnter a number: ")) except(ValueError):     print "That was not a number!" else:     print "You entered the number", num