Starting_Num = int(input('Please enter intial number: '))
Ending_Num = int(input('Please enter ending number: '))
for number in range(Starting_Num, (Ending_Num+1)):
if (number % 3 == 0):
print(number, '--', 3)
elif (number % 5 == 0):
print(number, '--', 5)
elif (number % 3 == 0) & (number % 5 == 0):
print(number, '-- both')
elif (number % 3 != 0) & (number % 5 != 0):
print(number)

- 55,782
- 14
- 81
- 108
-
I want to make the output to print 'both' if the number can be divided by 3 AND 5. – Ehab Alsaada May 06 '22 at 22:10
-
1`elif` is only executed if the preceding if conditions are false. – khelwood May 06 '22 at 22:10
-
So, swap the if statement with the elif statement that would output 'both'? – Ehab Alsaada May 06 '22 at 22:12
5 Answers
With the Python syntax, you need to use an and
instead of &
.
Test if a is greater than b, AND if c is greater than a:
a = 200 b = 33 c = 500 if a > b and c > a: print("Both conditions are True")

- 190
- 1
- 6
Does your program error at all? Python 3 allows keyword &
as well as and
to be used as "and" conditional statements. It should work either way...

- 1
- 1
-
Just to extend on this, `and` is more widely used and `&` will not work in some cases. You can read information about it on here. https://byjus.com/gate/difference-between-and-and-in-python/#:~:text=Basics-,The%20and%20is%20a%20type%20of%20Logical%20AND%20that%20returns,performs%20operations%20bit%20by%20bit. However in your case, it works either way. – thatsouris May 06 '22 at 22:20
You can use "and" or "or" operator, difference between them is that "and" should call True if all of the condition are True, "or" - if one of it is True.
example:
a = 1
b = 2
c = 'd'
if type(a) == int and type(b) == int:
# that will be printed
print("a, b are numbers")
if type(a) == int and type(c) == int:
# that will not be printed
print("a, c are numbers")
if type(a) == int or type(c) == int:
# that will be printed
print("a or c is number")
your code should look like:
Starting_Num = int(input('Please enter intial number: '))
Ending_Num = int(input('Please enter ending number: '))
for number in range(Starting_Num, (Ending_Num+1)):
if (number % 3 == 0):
print(number, '--', 3)
elif (number % 5 == 0):
print(number, '--', 5)
elif (number % 3 == 0) and (number % 5 == 0):
print(number, '-- both')
elif (number % 3 != 0) and (number % 5 != 0):
print(number)

- 41
- 3
The problem is where you position your 'and' condition. In your code, assuming the input fits the condition (e.g. the number is 15) what will happen is that you will enter the first condition: (if (number % 3 != 0):
) and skip all the others, because you used [if / elif] statements. What you need to do is move the condition [elif (number % 3 != 0) & (number % 5 != 0):
] to be the first condition, which will ensure that in case the number is divisible by both 3 and 5 - you will first check for both conditions.
Also, in case of logical and use the and
operator.
It should look something like this:
Starting_Num = int(input('Please enter intial number: '))
Ending_Num = int(input('Please enter ending number: '))
for number in range(Starting_Num, (Ending_Num+1)):
if (number % 3 == 0) and (number % 5 == 0):
print(number, '-- both')
elif (number % 3 == 0):
print(number, '--', 3)
elif (number % 5 == 0):
print(number, '--', 5)
else:
print(number)

- 21
- 8