I am trying to write a function that checks if an int passed into my method in the main file is divisible by 3 and 5.
I am having trouble because I am not sure what to use to check the condition in my method, since the value is passed in via the method call in the main file.
I am also not sure I am using the % operator correctly to check if the value is divisible by 3 and 5. Any guidance on this would be greatly appreciated.
Main:
from divisibleByPackage.isDivisibleBy import *
count_passed = 0
count_failed = 0
if (is_divisible(15) == True):
print("Test #1 passed")
count_passed = count_passed 1
else:
print("Test #1 FAILED")
count_failed = count_failed 1
if (is_divisible(1) == False):
print("Test #2 passed")
count_passed = count_passed 1
else:
print("Test #2 FAILED")
count_failed = count_failed 1
if (is_divisible(5) == False):
print("Test #3 passed")
count_passed = count_passed 1
else:
print("Test #3 FAILED")
count_failed = count_failed 1
if (is_divisible(0) == True):
print("Test #4 passed")
count_passed = count_passed 1
else:
print("Test #4 FAILED")
count_failed = count_failed 1
print(str(count_passed) " tests passed and " str(count_failed) " tests failed")
Function imported from PyDev package:
def is_divisible():
number1 = 3
number2 = 5
if (number1 % == 0 && number2 % == 0)
return True
else
return False
I was expecting the method call in my main to return true or false based on the condition, but Eclipse is saying my syntax is incorrect.
CodePudding user response:
number1 % 3 == 0
means a number is divisible by 3
number1 % 5 == 0
means a number is divisible by 5
number1 % 3 == 0 and number1 % 5 == 0
# NOTE it is NOT && for 'and' in python
means it is divisible by both 3 AND 5
number1 % 3 == 0 or number1 % 5 == 0
means it is divisible by either 3 or 5 or maybe both
number1 % == 0
is just a syntax error (also &&
is not correct in python)
CodePudding user response:
Your function needs to accept a parameter, which is the integer to be tested.
Also there's no need to declare 3 and 5 as separate variables. Just use 3 and 5.
Also also, you don't need to explicitly return true/false. You can return the result of the modulus operation, which is itself true/false.
def is_divisible(num):
return (num % 3 == 0) and (num % 5 == 0)
CodePudding user response:
There are three errors in the code :
- The function is defined not to take arguments, however in the tests, it is called with an argument.
&&
is not an operator in Python,and
is used instead.- The second operand to the
%
operator is missing. The%
is a binary operator, meaning it works with two operands. Example3%2
The function should be written as
def is_divisible(number):
if number % 3 == 0 and number % 5 == 0:
return True
else:
return False
CodePudding user response:
Other answers explain the wrongs. This code will work:
def is_divisible(number1):
if (number1 % 3 == 0 and number1 % 5 == 0):
return True
else:
return False
count_passed = 0
count_failed = 0
if (is_divisible(15) == True):
print("Test #1 passed")
count_passed = count_passed 1
else:
print("Test #1 FAILED")
count_failed = count_failed 1
if (is_divisible(1) == False):
print("Test #2 passed")
count_passed = count_passed 1
else:
print("Test #2 FAILED")
count_failed = count_failed 1
if (is_divisible(5) == False):
print("Test #3 passed")
count_passed = count_passed 1
else:
print("Test #3 FAILED")
count_failed = count_failed 1
if (is_divisible(0) == True):
print("Test #4 passed")
count_passed = count_passed 1
else:
print("Test #4 FAILED")
count_failed = count_failed 1
print(str(count_passed) " tests passed and " str(count_failed) " tests failed")