function that reverses digits, takes into consideration the sign
<p>I've looked on the site to try and figure out how to do this but I'm still stuck. </p>

<p>My function is supposed to reverse digits, so reverse_digits(8765) returns 5678. This is easily done by :</p>

<pre><code>def reverse_digits(num):
    return int(str(num)[::-1])
</code></pre>

<p>However, my code needs to 1) test if it is a negative and keep it negative (so -8765 returns -5678) and 2) I think I should test to see if num is actually an int. </p>

<p>So far I have </p>

<pre><code>def reverse_digits(num):
   num = str(num)[::-1]
   if num == '-':
       minus = 1
       num = num[:-1]
   else: 
       minus = 0    
   int(num)  
   if minus == 1:
       num = num*-1
   else:
       num = num   
   return num
</code></pre>

<p>It works for digits without a '-', but returns '' when it has a '-'. </p>

<p>I was originally trying to put the test to see if it is an int at the beginning od the loop like</p>

<pre><code>if (num != int):
    print("wrong type") 
    sys.exit()
else:
   (the rest of my above code)
</code></pre>

<p>but that wouldn't work for me. Should I put all the code in a while loop so I can use continue/break? 
Thanks!</p>

<p>Try to use the isdigit() string method.</p>

<pre><code>def reverse_digit(num):
      num_str = str(num)[::-1].strip('-')
      if not num_str.isdigit():
           &lt;do what u want&gt;
      if num &lt; 0:
         return -int(num_str)
      else:
         return int(num_str)
</code></pre>

python