Reputation: 53
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.
Upvotes: 0
Views: 459
Reputation: 36
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")
Upvotes: 0
Reputation: 95
There are three errors in the code :
&&
is not an operator in Python, and
is used instead.%
operator is missing. The %
is a binary operator, meaning it works with two operands. Example 3%2
The function should be written as
def is_divisible(number):
if number % 3 == 0 and number % 5 == 0:
return True
else:
return False
Upvotes: 1
Reputation: 33335
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)
Upvotes: 0
Reputation: 113978
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)
Upvotes: 0