1

So I have to write a program that takes two arguments, an integer 'num', and an integer 'limit'. It then has to return the list of divisors of num that are less than or equal. To break it down more, A divisor of 'num' is a number X between 1 and 'num' such that the remainder of dividing 'num' by X is 0. If any argument is not an integer, the function is supposed to return None.

here is an example of what I mean:

.....divisors("hello!", 5) should return None, because "hello" is a string

.....divisors("23", 5) should return None, because "23" is a string

......divisors(15, 12.34) should return None, because 12.34 is a float

......divisors(1, 5) should return [1]

......divisors(12, 5) should return [1, 2, 3, 4]

def divisors(num, limit):
   if num and limit not type(int):
        return None
   else:
       # I don't know what else to put here to make it divide and return the correct values....
Mazdak
  • 105,000
  • 18
  • 159
  • 188
Dom
  • 75
  • 1
  • 7

1 Answers1

0

You can use isinstance function to check the type of your arguments and then use yield to return a generator :

>>> def divisors(num, limit):
...   if isinstance(num,int) and isinstance(limit,int):
...         for i in range(1,limit):
...            if num%i==0:
...               yield i
...   else :
...        yield None
... 
>>> list(divisors(12, 5))
[1, 2, 3, 4]
>>> list(divisors('12', 5))
[None]
Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188