def number_from_user( prompt ): while True: spec = input( prompt ) try: return float( spec ) except ValueError: s = "Sorry, '{}' is not a valid number spec. Try e.g. '3.14'." print( s.format( spec ) ) print() print( "This program computes the sum of two numbers A and B." ) print() a = number_from_user( "Number A, please: " ) b = number_from_user( "Number B, please: " ) sum = a + b print() print( "{} + {} = {}".format( a, b, sum ) )