Good day! Please help me understand, I have a function:

def bin(n): if n == 0: return [] n, d = divmod(n, 2) return bin(n) + [d] 

it returns the binary format of the number (peculiar), further:

 n = input('int:') if n: try: n = int(n) except ValueError as err: print 'Error n' else: print 'n!' print bin(n) 

I need, when n is entered, if it is an object that is not converted to an integer, then process this error and display the message until the error is processed and I do not understand why. And how can I put all this code into one function? Thanks in advance for your help.

    2 answers 2

    You need to use the raw_input() function instead of input() .

    PS Try passing the value -1 to your bin() function and get endless recursion. And then I would make it easier:

     # bin() здесь - это встроенная функция python list(bin(n).split('b', 1)[1]) 

      As far as I remember there TypeError needs to be caught. Documentation .