-4
def div(num1,num2):
  n = 0
  while n > 0:
    if num1%n == 0 and num2%n==0:
        print(True)
        n+=1
    else:
        print(False)

This is probably wrong though.

EDIT:

def div(num1,num2):
if num1%num2==0:
    return True
else:
    return False
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Give proper intendation from n=0 onwards – Sagar V Mar 21 '17 at 01:24
  • 2
    What are you trying to do? You seem to be doing more than just testing if a number is divisible by *n*. – blacksite Mar 21 '17 at 01:25
  • That loop is never run because `n` always is 0. And if you changed the condition to `n >= 0`, that's just an infinite loop. Are you sure that is what you want? – Jerfov2 Mar 21 '17 at 01:26
  • maybe it should be n – jace Mar 21 '17 at 01:30
  • @Jerfov2 `n >= 0` wouldn't make it an infinite loop. It would crash right away. – Stefan Pochmann Mar 21 '17 at 01:31
  • @StefanPochmann why would it "crash"? – Jerfov2 Mar 21 '17 at 01:33
  • @Jerfov2 `ZeroDivisionError: integer division or modulo by zero` – Stefan Pochmann Mar 21 '17 at 01:33
  • @StefanPochmann Nice catch, I didn't see that. This is broken is more ways than one ;) – Jerfov2 Mar 21 '17 at 01:34
  • Technically any real number is divisible by any non-zero real number... Are you looking for only numbers whose modulus with `n` is `0`? – the_constant Mar 21 '17 at 01:40
  • possible exact dupe of [How do you check whether a number is divisible by another number (Python)?](http://stackoverflow.com/questions/8002217/how-do-you-check-whether-a-number-is-divisible-by-another-number-python) – chickity china chinese chicken Mar 21 '17 at 01:47
  • remove all the business with `n = 0`, `while n > 0:` and `n+=1`, you just need the one line `if num1 % num2 == 0:`, then put quotes around `"True"`, and `"False"` – chickity china chinese chicken Mar 21 '17 at 01:53
  • 1
    @downshift yes, I got it thank you so much. i know this is broken. This was my first semester in coding and i'm learning more and more everyday. this is what I have now and it is great. I was overcomplicating it. Thank you guys for everything! – Ibrahim Chalhoub Mar 21 '17 at 03:11
  • def div(num1,num2): if num1%num2==0: return True else: return False – Ibrahim Chalhoub Mar 21 '17 at 03:11
  • @IbrahimChalhoub, no problem, glad you figured it out, cheers! :) – chickity china chinese chicken Mar 21 '17 at 03:12
  • @downshift I just have a couple quick questions about python what do you think? 1. How many arguments are required to call a function? # 2. How many values are required to return from a function? I put at least one argument for calling a function and at least one value required to return from a function. I can't see how this is incorrect. Please correct me if I'm wrong as I am new to functions. – Ibrahim Chalhoub Mar 21 '17 at 03:16
  • @IbrahimChalhoub, No arguments are required for call to or return from functions. However, if you specify parameters in a function's signature, then that same number of parameters are required when you call the function. Same with returning values from a function, functions aren't required to return any value, but if you call a function and try to use a return value, but don't return anything from the function then it will error. But none are required. – chickity china chinese chicken Mar 21 '17 at 03:25
  • @IbrahimChalhoub you can have functions with no arguments and no return that you call to change the state of a global variable – litepresence Mar 21 '17 at 03:25
  • Hm, so I guess the technical answer is none to both. – Ibrahim Chalhoub Mar 21 '17 at 04:12

1 Answers1

0

this question is good use for ternary return

def div(number, n):
    # check if denominator is zero or modulo not 0
    return False if ((n==0) or (number%n!=0)) else True

print div(10,0),div(0,10),div(2,10),div(10,2)

>>>False True False True

possible duplicate

How do you check whether a number is divisible by another number (Python)?

Community
  • 1
  • 1
litepresence
  • 3,109
  • 1
  • 27
  • 35