Etrade Griffiths wrote: > Hi > > I want to check the type of a variable so that I know which format to use > for printing eg > > def print_correct_format(a): > > if type(a) == 'int': > print "a is an integer, value %i" % (a) > elif type(a) == 'float': > print "a is a float, value %f" % (a) > else: > print "a is unknown type" > > The comparison type(a) == 'int' etc does not work - I'm sure there's a > simple way to fix this but can't see it at the moment - any suggestions?
You need to compare to the actual type, not the name of the type: if type(a) == int: or if type(a) == float: You could also use if isinstance(a, int): which will succeed if a is an instance of a subclass of int as well. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor